参考: http://blog.csdn.net/skyharute/article/details/51404357#comments
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DLGTextChange) name:UITextViewTextDidChangeNotification object:nil];
self.myLabel=[[UILabel alloc]init];
self.myLabel.font=[UIFont systemFontOfSize:12];
self.myLabel.textColor=[UIColor blackColor];
[self.view addSubview:self.myLabel];
self.myLabel.attributedText=[self handleColorStr:@"您还可以输入140个字"];
[self.myLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(textview).offset(-6);
make.bottom.equalTo(textview).offset(-6);
}];
-(void)DLGTextChange{
static const NSInteger Max_Num_TextView = 140;
self.isContentTextViewEnable = true;
//获取当前键盘类型
UITextInputMode *mode = (UITextInputMode *)[UITextInputMode activeInputModes][0];
//获取当前键盘语言
NSString *lang = mode.primaryLanguage;
//如果语言是汉语(拼音)
if ([lang isEqualToString:@"zh-Hans"])
{
//取到高亮部分范围
UITextRange *selectedRange = [textview markedTextRange];
//取到高亮部分
UITextPosition *position = [textview positionFromPosition:selectedRange.start offset:0];
//如果取不到高亮部分,代表没有拼音
if (!position){
//当期超过最大限制时
if (textview.text.length > Max_Num_TextView) {
//对超出部分进行裁剪
textview.text = [textview.text substringToIndex:Max_Num_TextView];
//同时对可继续书写属性设为否,shouldChangeTextInRange方法会调用
self.isContentTextViewEnable = NO;
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"不能超过140个字";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:2];
//同时将下方提示label设置为0
self.myLabel.attributedText = [self handleColorStr:[NSString stringWithFormat:@"您还可以输入0个字"]];
}
//如果没超出,那么就计算剩余字数
self.myLabel.attributedText = [self handleColorStr:[NSString stringWithFormat:@"您还可以输入%d个字",(int)(Max_Num_TextView - textview.text.length)]];
}else{
//表示还有高亮部分,暂不处理
}
}else{
//如果语言不是汉语,直接计算
if (textview.text.length > Max_Num_TextView) {
textview.text = [textview.text substringToIndex:Max_Num_TextView];
self.isContentTextViewEnable = NO;
self.myLabel.attributedText = [self handleColorStr:[NSString stringWithFormat:@"您还可以输入0个字"]];
}
self.myLabel.attributedText = [self handleColorStr:[NSString stringWithFormat:@"您还可以输入%d个字",(int)(Max_Num_TextView-textview.text.length)]];
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@""]) {
return YES;
}else{
return self.isContentTextViewEnable;
}
}
//对字的位置颜色进行处理
-(NSMutableAttributedString*)handleColorStr:(NSString*)str{
NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc]initWithString:str];
if (attStr.length == 11) {
[attStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(6, 3)];
}else if(attStr.length == 10){
[attStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(6, 2)];
}else{
[attStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(6, 1)];
}
return attStr;
}