use*_*142 3 iphone xcode objective-c ios
如何在objective-c/interface builder中将字限制应用于UITextView.
我一直在寻找并找到字符数,但没有字数......
任何人都给我任何指针......
您可以只计算空格数并限制它.这是一个黑客,但它的工作原理.
你可以在里面做到这一点
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText
Run Code Online (Sandbox Code Playgroud)
通过使用这些NSString方法之一
- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
Run Code Online (Sandbox Code Playgroud)
例如,您可以按照以下方式执行某些操作:
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想要更准确,您还可以首先通过用单个空格替换多个空格并在标点符号后插入空格来"修复"文本.这可能应该写成您在输入文本上调用的单独函数.
| 归档时间: |
|
| 查看次数: |
2308 次 |
| 最近记录: |