iOS 帶有placeholder、clearButton和自動(dòng)計(jì)數(shù)的textView

由于項(xiàng)目中許多地方用到textView,并且要有placeholder和自動(dòng)計(jì)數(shù)、字?jǐn)?shù)限制,所以自己就想著寫(xiě)一個(gè),可以方便很多地方調(diào)用。

演示.gif
1.先新建一個(gè)繼承UITextView的文件,主要是在這里實(shí)現(xiàn)placeholder、clearButton以及自動(dòng)計(jì)數(shù)控件的通用類設(shè)置,還有一些代理方法:
@property (nonatomic, strong) NSString *placeholder;
@property (nonatomic, strong) UIColor *placeholderColor;
@property (nonatomic, strong) UIFont *placeholderFont;

//允許輸入的最大長(zhǎng)度
@property (nonatomic, assign) NSInteger maxLength;
//是否顯示 計(jì)數(shù)器 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.建一個(gè)繼承UIView的文件,在這里設(shè)置屬性和回調(diào)方法,把上面創(chuàng)建的頭文件導(dǎo)入進(jìn)來(lái),如下方法:
-(void)textViewDidChange:(UITextView *)textView
{
    [self textViewTextLengthChange:textView.text.length];
    
    self.maxTextCount = 60;
    
    NSString *toBeString = textView.text;
    
    NSString *lang = [(UITextInputMode*)[[UITextInputMode activeInputModes] firstObject] primaryLanguage]; // 鍵盤(pán)輸入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 簡(jiǎn)體中文輸入,包括簡(jiǎn)體拼音,健體五筆,簡(jiǎn)體手寫(xiě)
        UITextRange *selectedRange = [textView markedTextRange];
        //獲取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 沒(méi)有高亮選擇的字,則對(duì)已輸入的文字進(jìn)行字?jǐn)?shù)統(tǒng)計(jì)和限制
        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];
            
            
        } // 有高亮選擇的字符串,則暫不對(duì)文字進(jìn)行統(tǒng)計(jì)和限制
        else{
            
        }
    }
    // 中文輸入法以外的直接對(duì)其統(tǒng)計(jì)限制即可,不考慮其他語(yǔ)種情況
    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];
    }
}

  • 這里進(jìn)行了一個(gè)高亮判斷和輸入法的判斷,主要是在計(jì)數(shù)的時(shí)候,能夠在選中高亮文字之后再執(zhí)行,這樣就不會(huì)造成輸入字?jǐn)?shù)達(dá)到最大限制時(shí)計(jì)數(shù)label顯示不正確,或者是鍵盤(pán)直接不能用了,達(dá)到的效果就是字?jǐn)?shù)達(dá)到限制時(shí),鍵盤(pán)還能輸入,textView不再輸入或高亮選中無(wú)效
3.在需要的地方調(diào)用就可以了
 //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字以內(nèi)..." contentText:_inputText maxTextCount:60];
    __weak typeof (self) weakSelf = self;
    customTextView.frame = CGRectMake(weakSelf.view.frame.origin.x, 0, weakSelf.view.frame.size.width, 200);

具體代碼可直接去下載,有錯(cuò)誤或改進(jìn)的地方,請(qǐng)直接@我,不吝賜教。
代碼傳送門(mén):demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,157評(píng)論 4 61
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,316評(píng)論 0 17
  • 這只是我在學(xué)Android過(guò)程中對(duì)于所學(xué)知識(shí)的鞏固和方便日后查詢的學(xué)習(xí)筆記,能幫助到有需要的和我一樣的初學(xué)者就更好...
    TakeItEasyJQ閱讀 443評(píng)論 0 0
  • 我生氣,我真的生氣,我不懂,為什么,為什么不告訴我,關(guān)于你前女友的任何。 可能是嫉妒讓我發(fā)狂,你說(shuō),你倆大學(xué)在一起...
    你是馬里奧嘛閱讀 244評(píng)論 0 0
  • 下班之后,走在綠油油的小巷里?;丶业男腋8醒笠缭谀樕?。 最大的安慰,莫過(guò)于下班之后,坐在路邊,吃頓巴巴適適的火鍋與串串。
    小腳丫丫丫閱讀 200評(píng)論 0 1

友情鏈接更多精彩內(nèi)容