关于iOS对textView字数限制上的一些坑以及解决方法
2016-05-14 09:50
766 查看
现在的服务器一般是不会对字数做限制了,但是遇到一个需求需要对字数做限制,并且同时更新剩余的字数,刚开始我以为这个是非常简单的,然而这是个巨坑
这里一开始是使用代理方法监听字数的,然而这个方法当达到最大值200以后,虽然在英文状态下无法输入,但是在拼音联想状态下却依然会计算字数,并且由于拼音和拼音间存在空格,所以在高亮状态下也无法正常计算。
所以这里就采用了通知,这里计算还是比较准的,而且会出现一个-1的字数bug显示,但是真实的情况下是正常的,所以可以在字数达到限制后对提示字符进行写死限制,代码如下:
-(void)textChange:(NSNotification *)note{
static
const NSInteger Max_Num_TextView =
200;
self.isContentTextViewEnable =
true;
//获取当前键盘类型
UITextInputMode *mode = (UITextInputMode *)[UITextInputMode
activeInputModes][0];
//获取当前键盘语言
NSString *lang = mode.primaryLanguage;
//如果语言是汉语(拼音)
if ([lang
isEqualToString:@"zh-Hans"])
{
//取到高亮部分范围
UITextRange *selectedRange = [self.contentTextView
markedTextRange];
//取到高亮部分
UITextPosition *position = [self.contentTextView
positionFromPosition:selectedRange.start
offset:0];
//如果取不到高亮部分,代表没有拼音
if (!position){
//当期超过最大限制时
if (self.contentTextView.text.length
> Max_Num_TextView) {
//对超出部分进行裁剪
self.contentTextView.text = [self.contentTextView.text
substringToIndex:Max_Num_TextView];
//同时对可继续书写属性设为否,shouldChangeTextInRange方法会调用
self.isContentTextViewEnable =
NO;
//同时将下方提示label设置为0
self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入0个字"]];
}
//如果没超出,那么就计算剩余字数
self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入%ld个字",200-self.contentTextView.text.length]];
}else{
//表示还有高亮部分,暂不处理
}
}else{
//如果语言不是汉语,直接计算
if (self.contentTextView.text.length
> Max_Num_TextView) {
self.contentTextView.text = [self.contentTextView.text
substringToIndex:Max_Num_TextView];
self.isContentTextViewEnable =
NO;
self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入0个字"]];
}
self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入%ld个字",200-self.contentTextView.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;
}
这里一开始是使用代理方法监听字数的,然而这个方法当达到最大值200以后,虽然在英文状态下无法输入,但是在拼音联想状态下却依然会计算字数,并且由于拼音和拼音间存在空格,所以在高亮状态下也无法正常计算。
所以这里就采用了通知,这里计算还是比较准的,而且会出现一个-1的字数bug显示,但是真实的情况下是正常的,所以可以在字数达到限制后对提示字符进行写死限制,代码如下:
-(void)textChange:(NSNotification *)note{
static
const NSInteger Max_Num_TextView =
200;
self.isContentTextViewEnable =
true;
//获取当前键盘类型
UITextInputMode *mode = (UITextInputMode *)[UITextInputMode
activeInputModes][0];
//获取当前键盘语言
NSString *lang = mode.primaryLanguage;
//如果语言是汉语(拼音)
if ([lang
isEqualToString:@"zh-Hans"])
{
//取到高亮部分范围
UITextRange *selectedRange = [self.contentTextView
markedTextRange];
//取到高亮部分
UITextPosition *position = [self.contentTextView
positionFromPosition:selectedRange.start
offset:0];
//如果取不到高亮部分,代表没有拼音
if (!position){
//当期超过最大限制时
if (self.contentTextView.text.length
> Max_Num_TextView) {
//对超出部分进行裁剪
self.contentTextView.text = [self.contentTextView.text
substringToIndex:Max_Num_TextView];
//同时对可继续书写属性设为否,shouldChangeTextInRange方法会调用
self.isContentTextViewEnable =
NO;
//同时将下方提示label设置为0
self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入0个字"]];
}
//如果没超出,那么就计算剩余字数
self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入%ld个字",200-self.contentTextView.text.length]];
}else{
//表示还有高亮部分,暂不处理
}
}else{
//如果语言不是汉语,直接计算
if (self.contentTextView.text.length
> Max_Num_TextView) {
self.contentTextView.text = [self.contentTextView.text
substringToIndex:Max_Num_TextView];
self.isContentTextViewEnable =
NO;
self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入0个字"]];
}
self.tipLabel.attributedText = [self
handleColorStr:[NSString
stringWithFormat:@"您还可以输入%ld个字",200-self.contentTextView.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;
}
相关文章推荐
- ios开发之倒计时实现的方法
- iOS_代码规范
- 基于iOS核心绘图中的矩阵操作引发的思考
- iOS核心绘图中的矩阵操作
- iOS 调用系统相机和选择相册照片
- iOS解析lrc歌词文档
- iOS NSAttributedString和NSMutableAttributedString、NSMutableParagraphStyle的详细用法
- iOS : 静态库(.framework)合并
- iOS frame bounds center 区别
- iOS动画(Core Animation总结)
- [iOS]从URL远程请求图片
- IOS开发中如何自定义导航栏的按钮,文字和背景图片
- 如何在多台机器上共享ios证书
- 全局修改字体
- iOS TableView解耦
- iOS开发(OC)——文字滚动效果
- iOS开发之category动态添加属性的方法------关联
- iOS 并行编程:Thread
- Shou 团队诚意满满的招募 iOS Leader
- IOS绘制圆,直线,弧线,矩形,扇形,三角形,贝塞尔等图形