如何向NSTextView添加文本?使用Value属性赋值报错求助
解决NSTextView赋值报错的问题
你用TextView.Value = "String Value"报错是因为NSTextView根本没有Value属性,你看到的帖子大概率是把NSTextView和NSTextField的API搞混了,后者才有类似属性。在你当前用Visual Studio开发的C# Cocoa项目(Xamarin.Mac环境)里,给NSTextView设置文本有几种正确方式:
- 直接设置纯文本:
TextView.String = "String Value";
- 设置富文本(带样式):
// 示例:红色、14号系统字体的文本 var styledText = new NSAttributedString( "带样式的文本", new UIStringAttributes { ForegroundColor = NSColor.Red, Font = NSFont.SystemFontOfSize(14) } ); TextView.AttributedString = styledText;
- 追加文本(不覆盖原有内容):
如果需要在已有文本后添加内容,直接操作TextStorage更方便:
TextView.TextStorage.Append(new NSAttributedString("要追加的内容\n"));
内容的提问来源于stack exchange,提问作者Wiazarr
相关产品推荐
相关产品推荐

