我使用uitextview输入文本多行, 并限制文本输入行。 在iOS7中,当输入全文和键入多阶段字符(日语)时,应用程序将崩溃。 (我不能重复这个问题,但我的客户说,它显示了Crashlytics中的错误,与下图相同)
Crash Log Picture from Crashlytics
我无法调试,因为在测试模拟器中它不会崩溃。以下是我在下面设置文本限制行的代码:
- (BOOL) textView:(UITextView *)textViewshouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
CGRect textRect = [textView.layoutManager usedRectForTextContainer:textView.textContainer];
CGFloat sizeAdjustment = textView.font.lineHeight * [UIScreen mainScreen].scale;
//------------------fix num line-----------------------------
NSLayoutManager *layoutManager = [textView layoutManager];
unsigned numberOfLines, index, numberOfGlyphs = [layoutManager numberOfGlyphs];
NSRange lineRange;
for (numberOfLines=0, index=0; index < numberOfGlyphs; numberOfLines++) {
[layoutManager lineFragmentRectForGlyphAtIndex:index
effectiveRange:&lineRange];
index = NSMaxRange(lineRange);
}
if (textRect.size.height >= textView.frame.size.height - textView.contentInset.bottom - sizeAdjustment) {
if ([text isEqualToString:@"\n"] && numberOfLines < MAX_NUMBER_OF_LINES_ALLOWED-1) {
[UIView animateWithDuration:0.2 animations:^{
[textView setContentOffset:CGPointMake(textView.contentOffset.x, textView.contentOffset.y + sizeAdjustment)];
}];
}
}
[textView scrollRangeToVisible:textView.selectedRange];
if(numberOfLines < MAX_NUMBER_OF_LINES_ALLOWED) {
if([text isEqualToString:@"\n"] && numberOfLines == MAX_NUMBER_OF_LINES_ALLOWED-1) {
return NO;
} else {
return numberOfLines < MAX_NUMBER_OF_LINES_ALLOWED;
}
} else {
return NO;
}
}
-(void)textViewDidChange:(UITextView *)textView {
//------------------fix num line-----------------------------
NSLayoutManager *layoutManager = [textView layoutManager];
unsigned numberOfLines, index, numberOfGlyphs = [layoutManager numberOfGlyphs];
NSRange lineRange;
for (numberOfLines = 0, index = 0; index < numberOfGlyphs; numberOfLines++){
(void) [layoutManager lineFragmentRectForGlyphAtIndex:index
effectiveRange:&lineRange];
index = NSMaxRange(lineRange);
}
NSLog(@"numberOfLines %d",numberOfLines);
if (numberOfLines<MAX_NUMBER_OF_LINES_ALLOWED) {
_tempComment = self.commentTextView.text;
} else {
self.commentTextView.text = _tempComment;
}