NSTextView 中的自定义绘图选择
我有一个 NSTextView,我想在其中控制突出显示/选择的绘制。有人知道我必须覆盖哪种方法才能控制在选择矩形中绘制的内容吗?
I've got a NSTextView where I'd like to control drawing of the highlight/selection. Anyone know which method I have to overwrite so I can control what to draw in the selection rect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你没有指定你想画什么,所以很难给你一个直接的答案。
如果它只是一个属性字符串(字体、样式、颜色、背景),则不需要任何花哨的东西,只需查找 NSAttributedString 即可。有一些方法,例如
-setSelectedTextAttributes:
和委托方法-textViewDidChangeSelection:
一般来说,如果没有充分的理由,您不应该继承 NSTextView 。只需利用通常的委托机制,您几乎可以做任何事情。
如果它是非常自定义的东西,则有多种可能性,具体取决于您是否要自定义选择(
-setSelectedRange:
)绘制覆盖视图(获取 selectedRange 的框架)或乱搞低级布局引擎(-layoutManager
)。这确实取决于。You do not specify what you want to draw, so it's not easy to give you a straight answer.
If it's just an attributed string (Font, Style, Color, Background), you do not need anything fancy, just look for NSAttributedString. There are methods like
-setSelectedTextAttributes:
and the delegate method-textViewDidChangeSelection:
Generally, you should not subclass NSTextView if you don't have a very good reason to do so. You can do almost anything by just utilizing the usual delegation mechanisms.
If it's something very customized, there is a variety of possibilities depending on if you want to customize the selection (
-setSelectedRange:
) draw an overlay view (get the frame of the selectedRange) or mess around with the low level layout engine (-layoutManager
). It really depends.如果您不想使用标准属性来突出显示文本,例如通过使用 CALayer,您可以从 NSTextView 的布局管理器中获取包含所选内容的矩形。
If you don't want to use the standard attributes to highlight text, eg by using an CALayer, you can get the rects containing the selection from the layoutManager of the NSTextView.