今天设置UITextView的字数限制 在UITextViewDelegate的 - (void)textViewDidChange:(UITextView *)textView回调方法里操作报错
_NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 141
libc++abi.dylib: terminate_handler unexpectedly threw an exception
错误的做法:
- (void)textViewDidChange:(UITextView *)textView
{
// 评论字数不能超过140个
if (comment.length > kMaxLength) {
textView.text = [textView.text substringToIndex:kMaxLength];
}
}
正确的做法,操作UI要在主线程里面
- (void)textViewDidChange:(UITextView *)textView
{
// 评论字数不能超过140个
if (comment.length > kMaxLength) {
dispatch_async(dispatch_get_main_queue(), ^{
textView.text = [textView.text substringToIndex:kMaxLength];
});
}
}
这篇博客主要介绍了在iOS开发中遇到的NSLayoutTreeLineFragmentRectForGlyphAtIndex错误,导致应用崩溃的问题。错误源于在非主线程中修改了UI元素。提供了一个正确的解决方案,即确保修改TextView的文字内容操作在主线程内进行,避免触发异常。
583

被折叠的 条评论
为什么被折叠?



