iOS 带有placeholder、clearButton和自动计数的textView

由于项目中许多地方用到textView,并且要有placeholder和自动计数、字数限制,所以自己就想着写一个,可以方便很多地方调用。

演示.gif
1.先新建一个继承UITextView的文件,主要是在这里实现placeholder、clearButton以及自动计数控件的通用类设置,还有一些代理方法:
@property (nonatomic, strong) NSString *placeholder;
@property (nonatomic, strong) UIColor *placeholderColor;
@property (nonatomic, strong) UIFont *placeholderFont;

//允许输入的最大长度
@property (nonatomic, assign) NSInteger maxLength;
//是否显示 计数器 label
@property (nonatomic, assign) BOOL showWordCountLabel;
-(void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    [self setNeedsDisplay];
}

-(void)setPlaceholderFont:(UIFont *)placeholderFont
{
    _placeholderFont = placeholderFont;
    [self setNeedsDisplay];
}

-(void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    [self setNeedsDisplay];
}

2.建一个继承UIView的文件,在这里设置属性和回调方法,把上面创建的头文件导入进来,如下方法:
-(void)textViewDidChange:(UITextView *)textView
{
    [self textViewTextLengthChange:textView.text.length];
    
    self.maxTextCount = 60;
    
    NSString *toBeString = textView.text;
    
    NSString *lang = [(UITextInputMode*)[[UITextInputMode activeInputModes] firstObject] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length >= self.maxTextCount) {
                textView.text = [toBeString substringToIndex:self.maxTextCount];
            }
            self.countLabel.text=[NSString stringWithFormat:@"(%lu/%@)",(unsigned long)_textView.text.length, @(self.maxTextCount)];
            [self changeTextWithTextColor:[UIColor orangeColor] OfLabel:self.countLabel withLocation:1 andLength:self.countLabel.text.length-5];
            
            
        } // 有高亮选择的字符串,则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
    else{
        if (toBeString.length >= self.maxTextCount) {
            textView.text = [toBeString substringToIndex:self.maxTextCount];
        }
        self.countLabel.text=[NSString stringWithFormat:@"(%lu/%@)",(unsigned long)_textView.text.length, @(self.maxTextCount)];
        [self changeTextWithTextColor:[UIColor orangeColor] OfLabel:self.countLabel withLocation:1 andLength:self.countLabel.text.length-5];
    }
    
    if ([_delegate respondsToSelector:@selector(textViewDidChange:)]) {
        [_delegate textViewDidChange:textView];
    }
}

  • 这里进行了一个高亮判断和输入法的判断,主要是在计数的时候,能够在选中高亮文字之后再执行,这样就不会造成输入字数达到最大限制时计数label显示不正确,或者是键盘直接不能用了,达到的效果就是字数达到限制时,键盘还能输入,textView不再输入或高亮选中无效
3.在需要的地方调用就可以了
 //textView
    LALCustomTextView *customTextView =[[NSBundle mainBundle] loadNibNamed:@"LALCustomTextView" owner:self options:nil].lastObject;
    
    [self.view addSubview:customTextView];
    customTextView.delegate = self;
    customTextView.clearButtonType = ClearButtonAppearWhenEditing;
    [customTextView setPlaceholder:@"编辑新通知,60字以内..." contentText:_inputText maxTextCount:60];
    __weak typeof (self) weakSelf = self;
    customTextView.frame = CGRectMake(weakSelf.view.frame.origin.x, 0, weakSelf.view.frame.size.width, 200);

具体代码可直接去下载,有错误或改进的地方,请直接@我,不吝赐教。
代码传送门:demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,696评论 4 61
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,884评论 0 17
  • 这只是我在学Android过程中对于所学知识的巩固和方便日后查询的学习笔记,能帮助到有需要的和我一样的初学者就更好...
    TakeItEasyJQ阅读 3,025评论 0 0
  • 我生气,我真的生气,我不懂,为什么,为什么不告诉我,关于你前女友的任何。 可能是嫉妒让我发狂,你说,你俩大学在一起...
    你是马里奥嘛阅读 1,585评论 0 0
  • 下班之后,走在绿油油的小巷里。回家的幸福感洋溢在脸上。 最大的安慰,莫过于下班之后,坐在路边,吃顿巴巴适适的火锅与串串。
    小脚丫丫丫阅读 1,364评论 0 1

友情链接更多精彩内容