931743010 / BATextView

UITextView 的封装,可以自定义 placeholder、文字的(字体、颜色)、文字(字体、颜色),自适应高度,实时监测输入文字的最大高度,实时监测输入文字的最大个数,可以限制最大输入文字字数

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BATextView

BAHome Team Name

1、功能及简介

  • 1、可以自定义 placeholder的(字体、颜色)、文字(字体、颜色)
  • 2、可以自定义 输入文字的(字体、颜色)、文字(字体、颜色)
  • 3、可以自动布局,自适应高度,实时监测输入文字的最大高度
  • 4、可以实时监测输入文字的最大个数,可以限制最大输入文字字数
  • 5、用分类整理,无需改动源码即可实现各种自定义功能

2、图片示例

3、安装、导入示例和源码地址

  • 1、pod 导入【最新版本:】:
    pod 'BATextView'
    如果发现 pod search BATextView 搜索出来的不是最新版本,需要在终端执行 cd 转换文件路径命令退回到 desktop,然后执行 pod setup 命令更新本地spec缓存(可能需要几分钟),然后再搜索就可以了。
    具体步骤:
    • pod setup : 初始化
    • pod repo update : 更新仓库
    • pod search BATextView
  • 2、文件夹拖入:下载demo,把 BATextView 文件夹拖入项目即可,
  • 3、导入头文件:
    #import "BATextView.h"
  • 4、项目源码地址:
    OC 版 :https://github.com/BAHome/BATextView

4、BATextView 的类结构及 demo 示例

BATextView

BATextView.h

#ifndef BATextView_h
#define BATextView_h

#import "UITextView+BAKit.h"

#define BAKit_Objc_setObj(key, value) objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC)

#define BAKit_Objc_setObjCOPY(key, value) objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_COPY)

#define BAKit_Objc_getObj objc_getAssociatedObject(self, _cmd)

#define BAKit_Objc_exchangeMethodAToB(methodA,methodB) method_exchangeImplementations(class_getInstanceMethod([self class], methodA),class_getInstanceMethod([self class], methodB));

#pragma mark - NotiCenter
#define BAKit_NotiCenter [NSNotificationCenter defaultCenter]

/*!
 *  获取屏幕宽度和高度
 */
#define BAKit_SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)

#define BAKit_SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)

#pragma mark - weak / strong
#define BAKit_WeakSelf        @BAKit_Weakify(self);
#define BAKit_StrongSelf      @BAKit_Strongify(self);

/*!
 * 强弱引用转换,用于解决代码块(block)与强引用self之间的循环引用问题
 * 调用方式: `@BAKit_Weakify`实现弱引用转换,`@BAKit_Strongify`实现强引用转换
 *
 * 示例:
 * @BAKit_Weakify
 * [obj block:^{
 * @strongify_self
 * self.property = something;
 * }];
 */
#ifndef BAKit_Weakify
#if DEBUG
#if __has_feature(objc_arc)
#define BAKit_Weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
#else
#define BAKit_Weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
#endif
#else
#if __has_feature(objc_arc)
#define BAKit_Weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
#else
#define BAKit_Weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
#endif
#endif
#endif

/*!
 * 强弱引用转换,用于解决代码块(block)与强引用对象之间的循环引用问题
 * 调用方式: `@BAKit_Weakify(object)`实现弱引用转换,`@BAKit_Strongify(object)`实现强引用转换
 *
 * 示例:
 * @BAKit_Weakify(object)
 * [obj block:^{
 * @BAKit_Strongify(object)
 * strong_object = something;
 * }];
 */
#ifndef BAKit_Strongify
#if DEBUG
#if __has_feature(objc_arc)
#define BAKit_Strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
#else
#define BAKit_Strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
#endif
#else
#if __has_feature(objc_arc)
#define BAKit_Strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
#else
#define BAKit_Strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
#endif
#endif
#endif

/*!
 *********************************************************************************
 ************************************ 更新说明 ************************************
 *********************************************************************************
 
 欢迎使用 BAHome 系列开源代码 !
 如有更多需求,请前往:https://github.com/BAHome
 
 项目源码地址:
 OC 版 :https://github.com/BAHome/BATextView
 
 最新更新时间:2017-05-27 【倒叙】
 最新Version:【Version:1.0.0】
 更新内容:
 1.0.0.1、可以自定义 placeholder的(字体、颜色)、文字(字体、颜色)
 1.0.0.2、可以自定义 输入文字的(字体、颜色)、文字(字体、颜色)
 1.0.0.3、可以自动布局,自适应高度,实时监测输入文字的最大高度
 1.0.0.4、可以实时监测输入文字的最大个数,可以限制最大输入文字字数
 1.0.0.5、用分类整理,无需改动源码即可实现各种自定义功能

*/

#endif /* BATextView_h */

UITextView+BAKit.h

#import <UIKit/UIKit.h>

/**
 实时监测 TextView 输入文字,并返回当前文字最大高度,以便做自适应高度

 @param current_textViewHeight 当前文字的最大高度
 */
typedef void (^BAKit_TextView_HeightDidChangedBlock)(CGFloat current_textViewHeight);

/**
 实时监测 TextView 输入文字,并返回当前文字字符个数

 @param current_wordNumber 当前文字的字符个数
 */
typedef void (^BAKit_TextView_WordDidChangedBlock)(NSInteger current_wordNumber);

@interface UITextView (BAKit)

/**
 placeholder:文字
 */
@property(nonatomic, strong) NSString *ba_placeholder;

/**
 placeholder:文字颜色
 */
@property(nonatomic, strong) UIColor *ba_placeholderColor;

/**
 placeholder:文字字体
 */
@property(nonatomic, strong) UIFont *ba_placeholderFont;

/**
 文字字体,注意:一定要用 ba_textFont 设置,用系统的 self.font 设置无效
 */
@property(nonatomic, strong) UIFont *ba_textFont;

/**
 文字颜色,注意:一定要用 ba_textColor 设置,用系统的 self.textColor 设置无效
 */
@property(nonatomic, strong) UIColor *ba_textColor;

/**
 最大高度,如果需要随文字改变高度的时候使用
 */
@property (nonatomic, assign) CGFloat ba_maxHeight;

/**
 最小高度,如果需要随文字改变高度的时候使用
 */
@property (nonatomic, assign) CGFloat ba_minHeight;

/**
 实时监测 TextView 输入文字,并返回当前文字最大高度,以便做自适应高度
 */
@property(nonatomic, copy) BAKit_TextView_HeightDidChangedBlock ba_textView_HeightDidChangedBlock;

/**
 最大字数限制
 */
@property (nonatomic, assign) NSInteger ba_maxWordLimitNumber;

/**
 实时监测 TextView 输入文字,并返回当前文字字符个数
 */
@property(nonatomic, copy) BAKit_TextView_WordDidChangedBlock ba_textView_WordDidChangedBlock;


/**
 是否为空

 @return YES,NO
 */
- (BOOL)ba_textView_isEmpty;

/**
 快速设定自动布局

 @param maxHeight 最大高度
 @param minHeight 最小高度
 @param block BAKit_TextView_HeightDidChangedBlock
 */
- (void)ba_textView_autoLayoutWithMaxHeight:(CGFloat)maxHeight
                                  minHeight:(CGFloat)minHeight
                                      block:(BAKit_TextView_HeightDidChangedBlock)block;

/**
 快速设定最大字数限制返回当前字数

 @param limitNumber 最大字数限制
 @param block BAKit_TextView_WordDidChangedBlock
 */
- (void)ba_textView_wordLimitWithMaxWordLimitNumber:(NSInteger)limitNumber
                                              block:(BAKit_TextView_WordDidChangedBlock)block;

@end

demo 示例

// 示例1:
- (UITextView *)textView1
{
    if (!_textView1)
    {
        _textView1 = [UITextView new];
        _textView1.backgroundColor = BAKit_Color_Gray_11;
        /**
         文字字体,注意:一定要用 ba_textFont 设置,用系统的 self.font 设置无效
         */
        _textView1.ba_textFont = [UIFont systemFontOfSize:13];
        /**
         文字颜色,注意:一定要用 ba_textColor 设置,用系统的 self.textColor 设置无效
         */
        _textView1.ba_textColor = [UIColor purpleColor];
        
        _textView1.backgroundColor = BAKit_Color_Gray_11;
        // 自定义 placeholder
        _textView1.ba_placeholder = @"这里是 placeholder !";
        // 自定义 placeholder 颜色
        _textView1.ba_placeholderColor = [UIColor greenColor];
        // 自定义 placeholder 字体
        _textView1.ba_placeholderFont = [UIFont systemFontOfSize:16];
        
        [self.view addSubview:_textView1];
    }
    return _textView1;
}
    
// 示例2:
- (UITextView *)textView
{
    if (!_textView)
    {
        _textView = [UITextView new];
        _textView.userInteractionEnabled = YES;
        /**
         文字字体,注意:一定要用 ba_textFont 设置,用系统的 self.font 设置无效
         */
        _textView.ba_textFont = [UIFont systemFontOfSize:13];
        /**
         文字颜色,注意:一定要用 ba_textColor 设置,用系统的 self.textColor 设置无效
         */
        _textView.ba_textColor = [UIColor redColor];
        
        _textView.backgroundColor = BAKit_Color_Gray_11;
        // 自定义 placeholder
        _textView.ba_placeholder = @"这里是 placeholder !";
        // 自定义 placeholder 颜色
        _textView.ba_placeholderColor = [UIColor orangeColor];
        // 自定义 placeholder 字体
        _textView.ba_placeholderFont = [UIFont systemFontOfSize:25];
        
        /**
         快速设定自动布局
         
         @param maxHeight 最大高度
         @param minHeight 最小高度
         @param block BAKit_TextView_HeightDidChangedBlock
         */
        BAKit_WeakSelf
        [_textView ba_textView_autoLayoutWithMaxHeight:100 minHeight:60 block:^(CGFloat current_textViewHeight) {
            BAKit_StrongSelf
            self.textView_Height = current_textViewHeight;
            [self.view setNeedsLayout];
        }];
        
        /**
         快速设定最大字数限制返回当前字数
         
         @param limitNumber 最大字数限制
         @param block BAKit_TextView_WordDidChangedBlock
         */
        [_textView ba_textView_wordLimitWithMaxWordLimitNumber:60 block:^(NSInteger current_wordNumber) {
            BAKit_StrongSelf
            dispatch_async(dispatch_get_main_queue(), ^{
                self.label3.text = [NSString stringWithFormat:@"%ld/%ld", (long)current_wordNumber, (long)self.textView.ba_maxWordLimitNumber];
                [self.view setNeedsLayout];
            });
        }];
        [self.view addSubview:self.textView];
    }
    return _textView;
}

其他示例可下载demo查看源码!

5、更新记录:【倒叙】

欢迎使用 【BAHome】 系列开源代码 ! 如有更多需求,请前往:【https://github.com/BAHome】

最新更新时间:2017-05-27 【倒叙】
最新Version:【Version:1.0.0】
更新内容:
1.0.0.1、可以自定义 placeholder的(字体、颜色)、文字(字体、颜色)
1.0.0.2、可以自定义 输入文字的(字体、颜色)、文字(字体、颜色)
1.0.0.3、可以自动布局,自适应高度,实时监测输入文字的最大高度
1.0.0.4、可以实时监测输入文字的最大个数,可以限制最大输入文字字数
1.0.0.5、用分类整理,无需改动源码即可实现各种自定义功能

6、bug 反馈 和 联系方式

1、开发中遇到 bug,希望小伙伴儿们能够及时反馈与我们 BAHome 团队,我们必定会认真对待每一个问题!

2、联系方式
QQ群:479663605 【注意:此群为 2 元 付费群,介意的小伙伴儿勿扰!】
博爱QQ:137361770
博爱微博:

7、开发环境 和 支持版本

开发使用 Xcode Version 8.3.2 (8E2002) ,理论上支持所有 iOS 版本,如有版本适配问题,请及时反馈!多谢合作!

8、感谢

感谢 BAHome 团队成员倾力合作,后期会推出一系列 常用 UI 控件的封装,大家有需求得也可以在 issue 提出,如果合理,我们会尽快推出新版本!

BAHome 的发展离不开小伙伴儿的信任与推广,再次感谢各位小伙伴儿的支持!

About

UITextView 的封装,可以自定义 placeholder、文字的(字体、颜色)、文字(字体、颜色),自适应高度,实时监测输入文字的最大高度,实时监测输入文字的最大个数,可以限制最大输入文字字数

License:MIT License


Languages

Language:Objective-C 97.6%Language:Ruby 2.4%