UITextField和UITextView限制字數(包括中文)
一直以來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
// 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
[[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
.m//
// 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