gitDemo地址:https://github.com/laity1991/WKTextView
一.前言
- 之前做項目封裝textView時存在一些bug一直未解決:文本信息在用戶粘貼過長超出設(shè)定的最大字數(shù)時、帶emoji時截取顯示半個或亂碼(因為emoji在iOS中使用的是UTF16也就是占位符是8+8兩個字節(jié)的,占的長度為2,因此在計算字數(shù)時一個表情就占了2)
- 查了網(wǎng)上的一些封裝demo,或是不太全或是存在一些小bug,在此我整理封裝了一套WKTextView,方便大家使用和自己備用,主要解決問題:**
1.添加了占位文本 ,類似于textField的placeholder
2.中,英文字符輸入時限制。
3.帶emoji時截取顯示半個或亂碼字符處理。
4.處理了用戶在粘貼過來文本超出字數(shù)限制存在bug的情況**- ** 這里用到了我寫的UIView的一個分類UIView+WKCategory (重寫frame setter、getter方法),注釋比較詳細,不做過多贅述**
二 .WKTextView源碼
-
*** WKTextView.h ***
#import <UIKit/UIKit.h> @interface WKTextView : UITextView //文字 @property(nonatomic,copy) NSString *myPlaceholder; //文字顏色 @property(nonatomic,strong) UIColor *myPlaceholderColor; //最多輸入字數(shù) @property (nonatomic, assign) NSInteger maxNum; ///右下角統(tǒng)計字數(shù)label @property (nonatomic, strong) UILabel *countLabel; @end
-
*** WKTextView.m ***
#import "WKTextView.h"
#import "UIView+WKCategory.h"
@interface WKTextView()<UITextViewDelegate>
@property (nonatomic,weak) UILabel *placeholderLabel;
@end@implementation WKTextView - (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { UILabel *placeholderLabel = [[UILabel alloc]init]; [self addSubview:placeholderLabel]; placeholderLabel.textColor = [UIColor lightGrayColor]; self.placeholderLabel= placeholderLabel; UILabel *countLabel = [[UILabel alloc]init]; [self addSubview:countLabel]; countLabel.frame = CGRectMake(self.width - 60, self.height - 40, 60, 40); countLabel.textAlignment = NSTextAlignmentCenter; countLabel.textColor = [UIColor lightGrayColor]; countLabel.text = @"0/100"; self.countLabel = countLabel; ///設(shè)置邊框 self.layer.masksToBounds = YES; self.layer.cornerRadius = 4; self.layer.borderColor = [UIColor grayColor].CGColor; self.layer.borderWidth = 1; self.delegate = self; return self; } - (void)layoutSubviews{ [super layoutSubviews]; self.placeholderLabel.x = 8; self.placeholderLabel.y = 8; self.placeholderLabel.width = self.width - 2*self.placeholderLabel.x; ///根據(jù)占位文字myPlaceholder 算出占位Label的高度(寬度已定 高速自適應(yīng)) CGSize maxSize = CGSizeMake(self.placeholderLabel.width, MAXFLOAT); self.placeholderLabel.height = [self.myPlaceholder boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.placeholderLabel.font} context:nil].size.height; } ///箭頭textView的輸入文字變化 以此控制占位label的顯示和隱藏 - (void)textDidChange{ //hasText是UITextView的屬性 如果textView輸入了文字就是 YES 沒有文字就是NO self.placeholderLabel.hidden = self.hasText; } #pragma mark - UITextViewDelegate - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if ([text isEqualToString:@"\n"]) { [self resignFirstResponder]; return NO; } UITextRange *selectedRange = [textView markedTextRange]; //獲取高亮部分 UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0]; //獲取高亮部分內(nèi)容 //NSString * selectedtext = [textView textInRange:selectedRange]; //如果有高亮且當前字數(shù)開始位置小于最大限制時允許輸入 if (selectedRange && pos) { NSInteger startOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.start]; NSInteger endOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.end]; NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset); if (offsetRange.location < self.maxNum) { return YES; } else { return NO; } } NSString *comcatstr = [textView.text stringByReplacingCharactersInRange:range withString:text]; NSInteger caninputlen = self.maxNum - comcatstr.length; if (caninputlen >= 0) { return YES; } else { NSInteger len = text.length + caninputlen; //防止當text.length + caninputlen < 0時,使得rg.length為一個非法最大正數(shù)出錯 NSRange rg = {0,MAX(len,0)}; if (rg.length > 0) { NSString *s = @""; //判斷是否只普通的字符或asc碼(對于中文和表情返回NO) BOOL asc = [text canBeConvertedToEncoding:NSASCIIStringEncoding]; if (asc) { s = [text substringWithRange:rg];//因為是ascii碼直接取就可以了不會錯 } else { __block NSInteger idx = 0; __block NSString *trimString = @"";//截取出的字串 //使用字符串遍歷,這個方法能準確知道每個emoji是占一個unicode還是兩個 [text enumerateSubstringsInRange:NSMakeRange(0, [text length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock: ^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) { if (idx >= rg.length) { *stop = YES; //取出所需要就break,提高效率 return ; } trimString = [trimString stringByAppendingString:substring]; idx++; }]; s = trimString; } //rang是指從當前光標處進行替換處理(注意如果執(zhí)行此句后面返回的是YES會觸發(fā)didchange事件) [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]]; //既然是超出部分截取了,哪一定是最大限制了。 self.countLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)self.maxNum,(long)self.maxNum]; } return NO; } } - (void)textViewDidChange:(UITextView *)textView{ self.placeholderLabel.hidden = self.hasText; UITextRange *selectedRange = [textView markedTextRange]; //獲取高亮部分 UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0]; //如果在變化中是高亮部分在變,就不要計算字符了 if (selectedRange && pos) { return; } NSString *nsTextContent = textView.text; NSInteger existTextNum = nsTextContent.length; if (existTextNum > self.maxNum) { //截取到最大位置的字符(由于超出截部分在should時被處理了所在這里這了提高效率不再判斷) NSString *s = [nsTextContent substringToIndex:self.maxNum]; [textView setText:s]; } if (existTextNum > 100) { existTextNum = 100; } //不讓顯示負數(shù) self.countLabel.text = [NSString stringWithFormat:@"%ld/%ld",MAX(0, existTextNum), (long)self.maxNum ]; } #pragma mark - setter - (void)setMaxNum:(NSInteger)maxNum{ _maxNum = maxNum; self.countLabel.text = [NSString stringWithFormat:@"0/%ld", (long)_maxNum]; } - (void)setMyPlaceholder:(NSString *)myPlaceholder{ _myPlaceholder = myPlaceholder; self.placeholderLabel.text = _myPlaceholder; ///重新計算占位label frame [self setNeedsLayout]; } - (void)setMyPlaceholderColor:(UIColor *)myPlaceholderColor{ _myPlaceholderColor = myPlaceholderColor; self.countLabel.textColor = myPlaceholderColor; self.placeholderLabel.textColor = _myPlaceholderColor; } ///從寫TextView setFont方法 使占位labe、TextView、文字統(tǒng)計 Label Font一致 - (void)setFont:(UIFont *)font{ [super setFont:font]; self.placeholderLabel.font = font; self.countLabel.font = font; ///重新計算占位label frame [self setNeedsLayout]; } @end
-
*** Demo ***
- (void)prepareForUI{ ///添加到父控件 WKTextView *textView = [[WKTextView alloc]initWithFrame:CGRectMake(50, 50, self.view.width - 100, 200)]; ///設(shè)置文本輸入框的占位字符 textView.myPlaceholder = @"我是占位字符串..."; textView.font = [UIFont systemFontOfSize:14]; textView.maxNum = 100; [self.view addSubview:textView]; }
相關(guān)屬性都暴露了出來,可以自行設(shè)置,效果如下:
WKTextView.gif