Mrw*_*lfy 2 uitextview nsattributedstring ios nsrange nsrangeexception
尝试通过选择设置我的UIText视图的属性文本属性.几乎是有效的.使用下面的红色字体颜色设置文本的操作.这有时会起作用,但通常会出错:
因未捕获的异常'NSRangeException'而终止应用程序,原因:'NSMutableRLEArray objectAtIndex:effectiveRange :: out of bounds'
即使文本视图中的字符似乎多于所选范围所指示的字符,也会发生这种情况.
- (IBAction)setText:(id)sender {
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithAttributedString:myTextView.attributedText];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(p1,p2)];
myTextView.attributedText = string;
}
Run Code Online (Sandbox Code Playgroud)
p1和p2是所选文本的开头和结尾.它们是使用下面的代码生成的,它似乎按预期工作:
- (void)textViewDidChangeSelection:(UITextView *)textView {
UITextRange *selectedRange = [myTextView selectedTextRange];
p1 = [myTextView offsetFromPosition:myTextView.beginningOfDocument toPosition:[selectedRange start]];
p2 = [myTextView offsetFromPosition:myTextView.beginningOfDocument toPosition:[selectedRange end]];
}
Run Code Online (Sandbox Code Playgroud)
编辑:我在阅读@ borrrden的评论后解决了这个问题.而不是NSMakeRange(p1,p2)]我使用NSMakeRange(p1,p2-p1)].
| 归档时间: |
|
| 查看次数: |
1901 次 |
| 最近记录: |