我有一个UITextView和一个带有三个按钮的栏,可以切换粗体,斜体和下划线。 我已将TextView的文本设置为属性,并且它的“允许编辑属性”选项为YES。 虽然编辑所选文本的代码正常工作(这是一个例子):
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20];
NSDictionary *dict = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
//edit selection
NSRange selectedRange = _noteTextView.selectedRange;
[[_noteTextView textStorage] beginEditing];
[[_noteTextView textStorage] addAttribute:NSFontAttributeName value:font range:selectedRange];
[[_noteTextView textStorage] addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:selectedRange];
[[_noteTextView textStorage] endEditing];
[[_noteTextView textStorage] setAttributes:dict range:selectedRange];
我在textview指标设置属性时遇到问题。
[[_noteTextView textStorage] addAttribute:]
无效,或者我可能对该范围有问题。
有什么建议?
提前谢谢!
答案 0 :(得分:0)
尝试使用它来设置范围[textView rangeForUserParagraphAttributeChange];
它将设置整个段落的属性,而不仅仅是所选范围。
编辑: 如果插入符号位置之外有文本,则可能必须将范围扩展一个字符,以确保键入的后续字符将落在属性应用的范围内。
将属性应用于范围就是这样,它将它们应用于范围,而不是超出范围的任何文本类型。如果范围的长度是零(即没有选择),那么我的猜测是你的属性被应用于任何东西。