思路:
1、通過runtime添加兩個(gè)字符串屬性,供外接訪問。一個(gè)為設(shè)置占位字符串(placeholder)使用险耀,一個(gè)為設(shè)置最大文字個(gè)數(shù)(limitLength)使用累奈。
2、通過runtime將UITextView和兩個(gè)UILabel關(guān)聯(lián)起來,一個(gè)用做顯示占位字符串(placeholderLabel)使用,一個(gè)用做顯示最大文字個(gè)數(shù)(wordCountLabel)使用桥滨。
3、在占位字符串(placeholder)和最大文字個(gè)數(shù)(limitLength)的set方法中分別調(diào)用第二步中兩個(gè)UILabel的初始化方法喻括。
4邀杏、注冊(cè)兩個(gè)通知(通知的名字都叫:UITextViewTextDidChangeNotification,但監(jiān)聽方法不一樣):一個(gè)用作監(jiān)聽UITextView輸入字?jǐn)?shù)唬血,有則不顯示占位文字望蜡,沒有就顯示占位文字。另一個(gè)用作監(jiān)聽是否超過最大輸入字?jǐn)?shù)使用拷恨,沒有超過可繼續(xù)使用脖律,超過則截取到最大字?jǐn)?shù),后面的不顯示腕侄,也無法輸入小泉。(倘若提示用戶超出最大字?jǐn)?shù)勒叠,可以在通知的監(jiān)聽方法中判斷提示)
效果圖如下:
此分類使用的時(shí)候,只是在正常創(chuàng)建UITextView時(shí)多了兩個(gè)屬性設(shè)置:
self.addessalDetailTextView.placeholder = @"請(qǐng)?zhí)顚懺敿?xì)地址膏孟,不少于120個(gè)字";
self.addessalDetailTextView.limitLength = @120;
具體使用代碼如下(是不是代碼很少):
UITextView *textView = [[UITextView alloc] init];
textView.frame = CGRectMake(10, 10, 300, 150);
textView.center = self.view.center;
textView.placeholder = @"請(qǐng)?zhí)顚懺敿?xì)地址眯分,不多于120個(gè)字";
textView.limitLength = @120;
[self.view addSubview:textView];
也可以使用xib關(guān)聯(lián),簡單方便柒桑。
下面就看分類(UITextView+CLTextView)的具體實(shí)現(xiàn):
1弊决、UITextView+CLTextView.h的實(shí)現(xiàn),這也就是思路中的第一步
@property (nonatomic,strong) NSString *placeholder;//占位符
@property (copy, nonatomic) NSNumber *limitLength;//字?jǐn)?shù)限制
2魁淳、UITextView+CLTextView.m的實(shí)現(xiàn)
引入#import <objc/runtime.h>頭文件
@interface UITextView ()
@property (nonatomic,strong) UILabel *placeholderLabel;//占位符
@property (nonatomic,strong) UILabel *wordCountLabel;//計(jì)算字?jǐn)?shù)
@end
@implementation UITextView (YLTextView)
static NSString *PLACEHOLDLABEL = @"placelabel";
static NSString *PLACEHOLD = @"placehold";
static NSString *WORDCOUNTLABEL = @"wordcount";
static const void *limitLengthKey = &limitLengthKey;
#pragma mark -- setter/getter方法
//顯示占位符label的setter/getter方法
-(void)setPlaceholderLabel:(UILabel *)placeholderLabel {
objc_setAssociatedObject(self, &PLACEHOLDLABEL, placeholderLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UILabel *)placeholderLabel {
return objc_getAssociatedObject(self, &PLACEHOLDLABEL);
}
//顯示字?jǐn)?shù)label的setter/getter方法
- (void)setWordCountLabel:(UILabel *)wordCountLabel {
objc_setAssociatedObject(self, &WORDCOUNTLABEL, wordCountLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UILabel *)wordCountLabel {
return objc_getAssociatedObject(self, &WORDCOUNTLABEL);
}
//供外接訪問的占位字符串飘诗,以便設(shè)置占位文字的setter/getter方法
- (void)setPlaceholder:(NSString *)placeholder {
objc_setAssociatedObject(self, &PLACEHOLD, placeholder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self setPlaceHolderLabel:placeholder];
}
- (NSString *)placeholder {
return objc_getAssociatedObject(self, &PLACEHOLD);
}
//供外接訪問的最大輸入字?jǐn)?shù),設(shè)置最大文字個(gè)數(shù)的setter/getter方法
- (NSNumber *)limitLength {
return objc_getAssociatedObject(self, limitLengthKey);
}
- (void)setLimitLength:(NSNumber *)limitLength {
objc_setAssociatedObject(self, limitLengthKey, limitLength, OBJC_ASSOCIATION_COPY_NONATOMIC);
[self addLimitLengthObserver:[limitLength intValue]];
[self setWordcountLable:limitLength];
}
#pragma mark -- 配置占位符標(biāo)簽
- (void)setPlaceHolderLabel:(NSString *)placeholder {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextViewTextDidChangeNotification object:self];
// 占位字符
self.placeholderLabel = [[UILabel alloc] init];
self.placeholderLabel.font = [UIFont systemFontOfSize:13.];
self.placeholderLabel.text = placeholder;
self.placeholderLabel.numberOfLines = 0;
self.placeholderLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.placeholderLabel.textColor = [UIColor lightGrayColor];
CGRect rect = [placeholder boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame)-7, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13.]} context:nil];
self.placeholderLabel.frame = CGRectMake(7, 7, rect.size.width, rect.size.height);
[self addSubview:self.placeholderLabel];
self.placeholderLabel.hidden = self.text.length > 0 ? YES : NO;
}
#pragma mark -- 設(shè)置字?jǐn)?shù)限制標(biāo)簽
- (void)setWordcountLable:(NSNumber *)limitLength {
//字?jǐn)?shù)限制
self.wordCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.frame) - 65, CGRectGetHeight(self.frame) - 20, 60, 20)];
self.wordCountLabel.textAlignment = NSTextAlignmentRight;
self.wordCountLabel.textColor = [UIColor lightGrayColor];
self.wordCountLabel.font = [UIFont systemFontOfSize:13.];
if (self.text.length > [limitLength integerValue]) {
self.text = [self.text substringToIndex:[self.limitLength intValue]];
}
self.wordCountLabel.text = [NSString stringWithFormat:@"%lu/%@",(unsigned long)self.text.length,limitLength];
[self addSubview:self.wordCountLabel];
}
#pragma mark -- 注冊(cè)限制位數(shù)的通知
- (void)addLimitLengthObserver:(int)length {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitLengthEvent) name:UITextViewTextDidChangeNotification object:self];
}
#pragma mark -- 限制輸入的位數(shù)
- (void)limitLengthEvent {
if ([self.text length] > [self.limitLength intValue]) {
self.text = [self.text substringToIndex:[self.limitLength intValue]];
}
}
#pragma mark -- NSNotification
- (void)textFieldChanged:(NSNotification *)notification {
if (self.placeholder) {
self.placeholderLabel.hidden = YES;
if (self.text.length == 0) {
self.placeholderLabel.hidden = NO;
}
}
if (self.limitLength) {
NSInteger wordCount = self.text.length;
if (wordCount > [self.limitLength integerValue]) {
wordCount = [self.limitLength integerValue];
#pragma mark -- 想提示用戶輸入已經(jīng)超出最大字?jǐn)?shù)可以打開此行代碼
// UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"超過最大字?jǐn)?shù)" message:@"您已經(jīng)超出了最大字?jǐn)?shù),超出部分將無法顯示" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
// [view show];
}
self.wordCountLabel.text = [NSString stringWithFormat:@"%ld/%@",wordCount,self.limitLength];
}
}