Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何以编程方式更改UITextView的NSAttributedString,而不修改interface builder上设置的属性?_Ios_Objective C_Attributes_Uitextview_Nsattributedstring - Fatal编程技术网

Ios 如何以编程方式更改UITextView的NSAttributedString,而不修改interface builder上设置的属性?

Ios 如何以编程方式更改UITextView的NSAttributedString,而不修改interface builder上设置的属性?,ios,objective-c,attributes,uitextview,nsattributedstring,Ios,Objective C,Attributes,Uitextview,Nsattributedstring,我仅使用以下代码编辑文本,但它不起作用: - (void)updateTextViewWithText:(NSString *)newText { NSDictionary *attributes = [self.textView.attributedText attributesAtIndex:0 effectiveRange:NULL]; self.textView.attributedText = [[NSAttributedString alloc] init

我仅使用以下代码编辑文本,但它不起作用:

- (void)updateTextViewWithText:(NSString *)newText
{      
    NSDictionary *attributes = [self.textView.attributedText attributesAtIndex:0 effectiveRange:NULL];
    self.textView.attributedText = [[NSAttributedString alloc] initWithString:newText attributes:attributes];
}

它现在正在工作。问题是在界面生成器上,没有为textView选择属性“Selectable”,因此attributedText属性返回nil。

在我尝试代码示例时,它起了作用。这听起来像是您的textView IBOutlet未连接,或者在Interface Builder中您必须将文本从“普通”更改为“属性化”。

您看到了什么?文本视图是否正在更新?文本已更新,但interface builder中的字体大小和颜色已丢失并重置为默认值。问题是,在interface builder上,没有为textView选择属性“Selectable”,因此attributedText属性返回nil。我尝试了“普通”和“属性化”,这两种方法都有效。