【发布时间】:2013-09-25 17:55:20
【问题描述】:
我最近开始研究 NSAtributedString 的工作原理。我正在使用 NSAtributedString 构建类似于富文本编辑器的东西。我有一个自定义编写的 CustomRichTextEditor:UITextView,我想使用下面的代码覆盖 NSAtributedString 的默认属性:
UIFont *font = [UIFont systemFontOfSize:24.0];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,[UIColor darkGrayColor],NSForegroundColorAttributeName,nil];
NSMutableAttributedString* attrStr = [self.richTextEditor.attributedText mutableCopy];
[attrStr setAttributes:attributes
range:NSMakeRange(0, self.richTextEditor.attributedText.string.length-1)
];
self.richTextEditor.attributedText = attrStr;
但是,由于我的编辑器为空且范围未知,我崩溃并出现错误:
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds”
当范围未知时,如何正确设置 NSAtributedString 的字体和颜色?
【问题讨论】:
-
为什么不 self.richTextEditor.attributedText.string.length?你正在剪切最后一个字符。
-
问题很明显但是没有范围怎么解决呢?
标签: iphone ios objective-c nsattributedstring