4

我在 NSTextView 中使用 NSAttributedString 来突出显示文本中的某些区域。我发现更改背景颜色和文本颜色很容易,但我什至不知道从哪里开始绘制更复杂的东西,例如圆角或背景渐变或边框。

这是我要实现的目标的图片:

突出显示的文本模型

这在 web 视图中是完全可行的,但是对于一些额外的图形繁荣来说似乎有点过分了。

4

4 回答 4

4

我创建了一个 NSTextView 实现,该实现与您要创建的外观非常相似(仅缺少渐变),该实现可在此处获得: http: //github.com/aiman86/ANTaggedTextView在此处输入图像描述

这是一篇解释我的方法的博客文章:http: //aimannajjar.com/blog/1-How-to-Create-NSTextView-with-Facebook-like-Tags-Mentions.html#post

于 2015-09-27T03:08:01.900 回答
1

You can do rounded corner with Quartz lib :

[view.layer setCornerRadius:7.0];

Everyting is possible but not as easy as a stylesheet !

于 2012-10-31T08:18:13.000 回答
0

您可以通过以下两种方式之一设置要突出显示的文本范围的属性:

NSTextView *textView = ...;
NSRange rangeToHighlight = ...; // Range of characters in textView's textStorage
NSColor *highlightColor = ...;

// Method 1: less efficient
[textView.textStorage setAttributes:@{ NSForegroundColorAttributeName : highlightColor } range:rangeToHighlight];

// Method 2: more efficient but only works for attributes that don't affect layout, e.g. color, underline etc.
[textView.layoutManager addTemporaryAttribute:NSForegroundColorAttributeName value:highlightColor forCharacterRange:rangeToHighlight];

我不确定你是否可以使用渐变色,但纯色当然可以。

不过,这不会让你得到圆角。

您可以尝试使用 NSTextAttachmentCell,以便自定义渲染角落。

于 2015-01-15T09:24:40.737 回答
-1

这个问题有一些解决方法可能会对您有所帮助,创建一个带有圆角的 UIView,然后尝试在其中绘制 NSTextView,它应该可以工作。

于 2012-11-01T05:54:43.010 回答