如何在 Objective-C/interface builder 中对 UITextView 应用字数限制?
我已经搜索了一段时间,发现了字符数,但没有找到字数......
有谁能给我指点一下吗...
您可以只计算空格数并对其进行限制。这是一个 hack,但它确实有效。
你可以在里面做这个
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText
通过使用其中之一NSString
methods
- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
例如,您可以按照以下方式执行操作:
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText {
NSString* newText = [aTextView.text stringByReplacingCharactersInRange:aRange withString:aText];
NSString *trimmedText = [newText stringByReplacingOccurrencesOfString:@" " withString:@""];
if (newText.length - trimmedText.length > wordLimit) {
return NO;
} else {
return YES;
}
}
如果你想更准确,你也可以先“修复”文本,用一个空格替换多个空格,并在标点符号后插入一个空格。这可能应该编写为您在输入文本上调用的单独函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)