问题描述
- 计算textView的文字和字符数目
-
使用下面的代码计算文字数目:-(NSInteger) getTotalWords{ NSLog(@"Total Word %lu",[[_editor.attributedText string]length]); if ([[_editor.attributedText string]length]==0) { return 0; } NSString *str =[_editor textInRange:[_editor textRangeWithRange:[self visibleRangeOfTextView:_editor]]]; NSInteger sepWord = [[[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsSeparatedByString:@" "] count]; sepWord += [[[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsSeparatedByString:@"n"] count]; sepWord=sepWord-2; return sepWord; }
下面是计算字符总数的代码:
-(NSInteger) getTotalChars{ NSString *str =[_editor textInRange:[_editor textRangeWithRange:[self visibleRangeOfTextView:_editor]]]; NSLog(@"%@",str); NSInteger charCount= [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]length]; return charCount=charCount-1; }
如果text超过两行,结果就会有误,会把新的一行作为文字。
谢谢您的帮忙。
解决方案
http://www.dewen.io/q/5423
时间: 2024-12-01 00:37:11