实现UITextView的placeholder及限制输入次数(干货)
2017-09-15 15:17
369 查看
众所周知, UITextfield有一个属性可以设置其placeholder, 但是UITextView却没有, 所以往往遇到要设置它的占位字符时,都要百度一番~~ ,今天又遇到这个问题, 所以自己写了一个Catgorty来实现这个需求, 分享出来希望能帮助一些伙伴! 好了, 话不多说, 上菜上菜
.h文件
.m文件
设置的时候,直接设置.h文件的两个属性即可!
.h文件
@property (nonatomic,strong) NSString *placeholder;//占位符 @property (copy, nonatomic) NSNumber *limitLength;//限制字数
.m文件
@interface UITextView () @property (nonatomic,strong) UILabel *placeholderLabel;//占位符 @property (nonatomic,strong) UILabel *wordCountLabel;//计算字数 @end @implementation UITextView (RSTextViewSetting) static NSString *PLACEHOLDLABEL = @"placelabel"; static NSString *PLACEHOLD = @"placehold"; static NSString *WORDCOUNTLABEL = @"wordcount"; static const void *limitLengthKey = &limitLengthKey; #pragma mark -- set/get... -(void)setPlaceholderLabel:(UILabel *)placeholderLabel { objc_setAssociatedObject(self, &PLACEHOLDLABEL, placeholderLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (UILabel *)placeholderLabel { return objc_getAssociatedObject(self, &PLACEHOLDLABEL); } - (void)setPlaceholder:(NSString *)placeholder { objc_setAssociatedObject(self, &PLACEHOLD, placeholder, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self setPlaceHolderLabel:placeholder]; } - (NSString *)placeholder { return objc_getAssociatedObject(self, &PLACEHOLD); } - (UILabel *)wordCountLabel { return objc_getAssociatedObject(self, &WORDCOUNTLABEL); } - (void)setWordCountLabel:(UILabel *)wordCountLabel { objc_setAssociatedObject(self, &WORDCOUNTLABEL, wordCountLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (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 -- 配置占位符标签 - (void)setPlaceHolderLabel:(NSString *)placeholder { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextViewTextDidChangeNotification object:self]; if (self.placeholderLabel) { [self.placeholderLabel removeFromSuperview]; } /* * 占位字符 */ 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 = UGUColorFromRGB_0x(0xD6D6D6); 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; self.wordCountLabel.text = [NSString stringWithFormat:@"%lu/%@",(unsigned long)self.text.length,self.limitLength]; } #pragma mark -- 配置字数限制标签 - (void)setWordcountLable:(NSNumber *)limitLength { if (self.wordCountLabel) { [self.wordCountLabel removeFromSuperview]; } /* * 字数限制 */ self.wordCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 110, CGRectGetHeight(self.frame) - 35, 65, 35)]; self.wordCountLabel.textAlignment = NSTextAlignmentRight; self.wordCountLabel.textColor = [UIColor lightGrayColor]; self.wordCountLabel.font = [UIFont systemFontOfSize:14]; 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 -- 增加限制位数的通知 - (void)addLimitLengthObserver:(int)length { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitLengthEvent) name:UITextViewTextDidChangeNotification object:self]; } #pragma mark -- 限制输入的位数 - (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]; } self.wordCountLabel.text = [NSString stringWithFormat:@"%zd/%@",wordCount,self.limitLength]; } } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self]; }
设置的时候,直接设置.h文件的两个属性即可!
相关文章推荐
- UITextFiled & UITextView 控制输入长度、中文处理以及UITextView 的placeHolder 的一种实现方法
- 限制输入次数的系统登陆的实现
- UITextView & UITextField 实现中英文混合输入的限制
- UITextField 和 UITextView实现字数限制 输入若干字以后就无法继续输入的功能
- Android Edittext输入限制一位小数2位小数(n位)小数简单通用实现
- 限制input输入类型(多种方法实现)
- 限制输入的次数用到while循环
- CAS通过用户名与ip限制密码输入错误次数
- 如何实现对UITextField ,UITextView等输入框的 字数限制
- 限制input输入类型(多种方法实现)
- 正则表达式实现 限制文字输入个数汉子,数字,英文单词
- 用javascript实现控制一个文本框的输入字数限制,超出字数限制文本框飘红显示-面试题
- jquery实现输入字数统计以及限制
- 如何实现对UITextField ,UITextView等输入框的 字数限制
- iOS控件UITextView 实现placeholder效果和解决UITableView键盘遮挡
- js实现文本框限制输入数字和小数点--兼容多个浏览器
- 如何实现对UITextField ,UITextView等输入框的 字数限制
- charCount.js实现输入文字限制
- UITextView 实现placeholder的方法
- 给系统密码添上一把限制锁限制输入次数防止无聊人猜密