一直以来UITextField和UITextView的字数显示就困扰着我,特别当输入为中文输入法时
今天再搜索这个问题时看到了2位同学的博客,通过整合完成了对UITextField和UITextView的中文输入法字数限制
先感谢这两位同学http://blog.sina.com.cn/s/blog_60f977e70101g4gj.html && http://www.tuicool.com/articles/BJZRV3i
贴上代码,方法需要时候来取
UITextField+limitLength.h
.h
// UITextField+limitLength.h
//
// Created by ZV on 16/1/9.
// Copyright © 2016年 zv. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UITextField (limitLength)
/**
* 使用时只要调用此方法,加上一个长度(int),就可以实现了字数限制
*
* @param length
*/
- (void)limitTextLength:(int)length;
/**
* uitextField 抖动效果
*/
@end
.m
// UITextField+limitLength.m
//
// Created by ZV on 16/1/9.
// Copyright © 2016年 zv. All rights reserved.
//
#import "UITextField+limitLength.h"
#import <objc/runtime.h>
@implementation UITextField (limitLength)
static NSString *kLimitTextLengthKey = @"kLimitTextLengthKey";
- (void)limitTextLength:(int)length
{
objc_setAssociatedObject(self, (__bridge const void *)(kLimitTextLengthKey), [NSNumber numberWithInt:length], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldTextLengthLimit:)
name:UITextFieldTextDidChangeNotification
object:self];
}
- (void)textFieldTextLengthLimit:(NSNotification *)sender
{
UITextField *textField = (UITextField *)sender.object;
NSNumber *lengthNumber = objc_getAssociatedObject(self, (__bridge const void *)(kLimitTextLengthKey));
int length = [lengthNumber intValue];
NSString *toBeString = textField.text;
NSString *lang = [[textField textInputMode] primaryLanguage];; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [textField markedTextRange];
//获取高亮部分
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position) {
if (toBeString.length > length)
{
textField.text = [toBeString substringToIndex:length];
}
}
// 有高亮选择的字符串,则暂不对文字进行统计和限制
else
{
}
}
// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
else{
if (toBeString.length > length) {
textField.text = [toBeString substringToIndex:length];
}
}
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self
name:@"UITextFieldTextDidChangeNotification"
object:self];
}
@end
UITextView+limitLength.h
.h
//
// UITextView+limitLength.h
//
// Created by ZV on 16/1/9.
// Copyright © 2016年 zv. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UITextView (limitLength)
/**
* 使用时只要调用此方法,加上一个长度(int),就可以实现了字数限制
*
* @param length
*/
- (void)limitTextLength:(int)length;
/**
* uitextField 抖动效果
*/
@end
//
// UITextView+limitLength.m
//
// Created by ZV on 16/1/9.
// Copyright © 2016年 zv. All rights reserved.
//
#import "UITextView+limitLength.h"
#import <objc/runtime.h>
@implementation UITextView (limitLength)
static NSString *kLimitTextLengthKey = @"kLimitTextLengthKey";
- (void)limitTextLength:(int)length
{
objc_setAssociatedObject(self, (__bridge const void *)(kLimitTextLengthKey), [NSNumber numberWithInt:length], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldTextLengthLimit:)
name:UITextViewTextDidChangeNotification
object:self];
}
- (void)textFieldTextLengthLimit:(NSNotification *)sender
{
UITextView *textView = (UITextView *)sender.object;
NSNumber *lengthNumber = objc_getAssociatedObject(self, (__bridge const void *)(kLimitTextLengthKey));
int length = [lengthNumber intValue];
NSString *toBeString = textView.text;
NSString *lang = [[textView textInputMode] primaryLanguage];; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [textView markedTextRange];
//获取高亮部分
UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position) {
if (toBeString.length > length)
{
textView.text = [toBeString substringToIndex:length];
}
}
// 有高亮选择的字符串,则暂不对文字进行统计和限制
else
{
}
}
// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
else{
if (toBeString.length > length) {
textView.text = [toBeString substringToIndex:length];
}
}
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self
name:@"UITextFieldTextDidChangeNotification"
object:self];
}
@end