//
注册监听方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChanged:) name:UITextViewTextDidChangeNotification object:self.replyTextView];
/**
* 计算输入的字数 , 超出规定字数不允许输入
* 计算输入的字数 , 超出规定字数不允许输入
*
* @param noti 通知
*/
- (
void
)textViewChanged:(
NSNotification
*)noti
{
NSString * textContent = self . replyTextView . text ;
int existTextNum = [textContent lengthForCString ];;
NSString *language = [ self . replyTextView . textInputMode primaryLanguage ];
if ([language isEqualToString : @"zh-Hans" ]) {
// 中文
UITextRange * selectedRange = [ self . replyTextView markedTextRange ];
UITextPosition * position = [ self . replyTextView positionFromPosition :selectedRange. start offset : 0 ];
if (!position) {
// 没有选中字符
if (existTextNum > TextViewMaxLength ) {
// 大于 200 字符
textContent = [textContent subStringWithByteLength : TextViewMaxLength ];
existTextNum = TextViewMaxLength ;
self . replyTextView . text = textContent;
} else
{
// 小于 200 字符
// existTextNum = [textContent lengthForCString];
}
} else
{
// 有选中字符
// existTextNum = [textContent lengthForCString];
}
} else
{
// 英文
{
NSString * textContent = self . replyTextView . text ;
int existTextNum = [textContent lengthForCString ];;
NSString *language = [ self . replyTextView . textInputMode primaryLanguage ];
if ([language isEqualToString : @"zh-Hans" ]) {
// 中文
UITextRange * selectedRange = [ self . replyTextView markedTextRange ];
UITextPosition * position = [ self . replyTextView positionFromPosition :selectedRange. start offset : 0 ];
if (!position) {
// 没有选中字符
if (existTextNum > TextViewMaxLength ) {
// 大于 200 字符
textContent = [textContent subStringWithByteLength : TextViewMaxLength ];
existTextNum = TextViewMaxLength ;
self . replyTextView . text = textContent;
} else
{
// 小于 200 字符
// existTextNum = [textContent lengthForCString];
}
} else
{
// 有选中字符
// existTextNum = [textContent lengthForCString];
}
} else
{
// 英文
// existTextNum = [textContent lengthForCString];
if
(existTextNum >
TextViewMaxLength
)
{
//
大于
200
字符
textContent = [textContent
subStringWithByteLength
:
TextViewMaxLength
];
existTextNum =
TextViewMaxLength
;
self
.
replyTextView
.
text
= textContent;
}
}
self . currentCountLabel . text = [ NSString stringWithFormat : @"%d" , existTextNum];
self . currentCountLabel . text = [ NSString stringWithFormat : @"%d" , existTextNum];
}
//下面这两个个都是分类里的方法,写个NSString的分类放里面用就可以了。
//这个方法从网上找的。
/**
* 计算字符串字节长度 , 中文一个字节,英文 0.5 字节
*
* @return 字节长度
*/
- ( int )lengthForCString
{
int strlength = 0 ;
char * p = ( char *)[ self cStringUsingEncoding : NSUnicodeStringEncoding ];
for ( int i = 0 ; i < [ self lengthOfBytesUsingEncoding : NSUnicodeStringEncoding ]; i ++) {
// NSLog(@"*p:%c",*p);
if (* p) {
p ++;
strlength ++;
}
else {
p ++;
}
}
return (strlength + 1 )/ 2 ;
* 计算字符串字节长度 , 中文一个字节,英文 0.5 字节
*
* @return 字节长度
*/
- ( int )lengthForCString
{
int strlength = 0 ;
char * p = ( char *)[ self cStringUsingEncoding : NSUnicodeStringEncoding ];
for ( int i = 0 ; i < [ self lengthOfBytesUsingEncoding : NSUnicodeStringEncoding ]; i ++) {
// NSLog(@"*p:%c",*p);
if (* p) {
p ++;
strlength ++;
}
else {
p ++;
}
}
return (strlength + 1 )/ 2 ;
}
//这个是自己写的。
/**
* 按字节数截取字符串,中文一个字节,英文 0.5 字节,如果最后字符是半个中文,自动抛掉最后一个字符
*
* @param length 字节数
*
* @return 截取后的字符串
*/
- ( NSString *)subStringWithByteLength:( int )length
{
float bytesCount = 0 ;
float lastBytesCount = 0 ;
for ( int i = 1 ; i <= self . length ; i ++) {
// NSLog(@"byteLength:%ld", [[self substringWithRange:NSMakeRange(i - 1, 1)] lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
if ([[ self substringWithRange : NSMakeRange (i - 1 , 1 )] lengthOfBytesUsingEncoding : NSUTF8StringEncoding ] == 3 ) {
bytesCount ++;
} else
{
bytesCount += 0.5 ;
}
if (bytesCount > length) {
// NSLog(@"byteCount:%f", lastBytesCount);
return [ self substringWithRange : NSMakeRange ( 0 , i - 1 )];
} else if (bytesCount == length)
{
return [ self substringWithRange : NSMakeRange ( 0 , i)];
} else
{
lastBytesCount = bytesCount;
}
}
return @" 按字节截取字符串出错,请检查截取字节是否超出范围。 " ;
* 按字节数截取字符串,中文一个字节,英文 0.5 字节,如果最后字符是半个中文,自动抛掉最后一个字符
*
* @param length 字节数
*
* @return 截取后的字符串
*/
- ( NSString *)subStringWithByteLength:( int )length
{
float bytesCount = 0 ;
float lastBytesCount = 0 ;
for ( int i = 1 ; i <= self . length ; i ++) {
// NSLog(@"byteLength:%ld", [[self substringWithRange:NSMakeRange(i - 1, 1)] lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
if ([[ self substringWithRange : NSMakeRange (i - 1 , 1 )] lengthOfBytesUsingEncoding : NSUTF8StringEncoding ] == 3 ) {
bytesCount ++;
} else
{
bytesCount += 0.5 ;
}
if (bytesCount > length) {
// NSLog(@"byteCount:%f", lastBytesCount);
return [ self substringWithRange : NSMakeRange ( 0 , i - 1 )];
} else if (bytesCount == length)
{
return [ self substringWithRange : NSMakeRange ( 0 , i)];
} else
{
lastBytesCount = bytesCount;
}
}
return @" 按字节截取字符串出错,请检查截取字节是否超出范围。 " ;
}