NSTextView 不可编辑的文本区域?
我有一个 NSTextView,其中包含供用户编辑的数据,但我想用不可编辑数据的页眉和页脚包围它,以便让用户了解上下文。
我认为 NSTextView
无法处理混合可编辑/不可编辑数据的概念,因此我提出了一些想法。
a) 使用带有自定义单元格的文本附件来绘制页眉和页脚。
b) 在 NSScrollView
中有 3 个 NSTextView
。
c)使用属性来确定哪些内容不能编辑,并使用委托方法来防止编辑,这可能是我最喜欢的,因为它可能是侵入性最小的。
我错过了什么,有什么更好的想法吗?
I have an NSTextView
that contains data for the user to edit, but I want to surround it with a header and footer of non-editable data to give the user an idea of context.
I don't think an NSTextView
can handle the concept of mixed editable/non-editable data, so I've come up with a few ideas.
a) Use text attachments with a custom cell to draw the header and footers.
b) Have 3 NSTextView
s within the NSScrollView
.
c) Use attributes to determine what cannot be edited,and use the delegate methods to prevent editing, this is probably my favourite, as it's probably the least intrusive.
Am I missing anything, any better ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSTextView 委托方法 -textView:shouldChangeTextInRange:replacementString: 会让你做到这一点。你可以“直接说不”来改变。 ;-)
更新/阐述(2015 年 11 月)
为了根据此答案的评论进行阐述,我们的想法是在 属性字符串 您的文本视图正在编辑。超越 标准属性,您可以指定自己的属性名称(任何
NSString
)和PLIST 兼容对象 作为该名称的值。例如,如果您想将一系列文本指定为“不可编辑”,您可以为该范围添加一个属性,属性名为(例如)
@"TextIsEditableAttributeName"
带有BOOL
值为YES
或NO
的NSNumber
:[NSNumber NO]
或@( NO )
(使用 ObjC 数字装箱 - 相同的结果:一个 NSNumber 实例)。稍后,当文本视图询问其委托是否应更改范围内的文本时,您可以检查该范围是否存在@"TextIsEditableAttributeName"
属性。实际上,只需将一个属性分配给不可编辑的范围,因此您甚至不必检查该值。您可以在此处放置一个空的 NSData 实例作为占位符,以便该属性具有值。您的属性名称可以是
@"EditingLocked"
或其他名称。这意味着您只需检查@"EditingLocked"
属性是否存在在建议范围内的任何位置并返回NO< /code> 当文本视图询问时。这将捕获可编辑范围与不可编辑范围的重叠选择(如果您允许选择复制不可编辑文本)。
当然,同样的方法也适用于
-textView:willChangeSelectionFromCharacterRanges:toCharacterRanges:
,这是另一种委托方法,允许您返回“更正”的范围值数组以供选择。如果您不想允许选择不可编辑的文本,则可以“剪切”在建议范围中找到的@"EditingLocked"
属性的任何实例所描述的范围。我希望这有帮助。
The NSTextView delegate method -textView:shouldChangeTextInRange:replacementString: will let you do this. You can "just say NO" to change. ;-)
Update / Elaboration (November, 2015)
To elaborate based on the comments on this answer, the idea is to use your own custom attributes on the attributed string your text view is editing. Beyond the standard attributes, you can specify your own attribute name (any
NSString
) and PLIST-compatible object as the value for that name.For example, if you wanted to designate a range of text as "uneditable", you could add an attribute for that range with an attribute named (for example)
@"TextIsEditableAttributeName"
with anNSNumber
with aBOOL
value ofYES
orNO
:[NSNumber NO]
or@( NO )
(to use ObjC number boxing - same result: an NSNumber instance). Later, when the text view asks its delegate if it should change text in range, you can inspect the range for the presence of your@"TextIsEditableAttributeName"
attribute.Really, there's only a need to assign an attribute to ranges that aren't editable, so you don't even have to check for the value. You could just put an empty
NSData
instance there for a placeholder so the attribute has a value. Your attribute name could be@"EditingLocked"
or something. This means you only have to check for the presence of the@"EditingLocked"
attribute anywhere in the proposed range and returnNO
when the text view asks. This would catch overlapped selection (if you allow selection for copying the non-editable text) of editable vs. non-editable ranges.This same approach, of course, can work for
-textView:willChangeSelectionFromCharacterRanges:toCharacterRanges:
, another delegate method that allows you to return a "corrected" array of range values for selection. If you don't want to allow non-editable text to be selected, you can "cut out" the ranges described by any instances of your@"EditingLocked"
attribute you find in the proposed ranges.I hope this helps.