TextIsEditableAttributeName

TextIsEditableAttributeName

本文介绍了NSTextView文本的不可编辑区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSTextView 包含供用户编辑的数据,但我想用不可编辑的数据的页眉和页脚包围它,以给用户一个上下文的概念。



我不认为一个 NSTextView 可以处理混合可编辑/不可编辑的概念数据,所以我想出了一些想法。



a)使用自定义单元格的文本附件来绘制页眉和页脚。



b)在 NSScrollView 中有3 NSTextView b
$ b

c)使用属性来确定什么不能被编辑,并使用委托方法来防止编辑,这可能是我最喜欢的,因为它可能是最少的侵入。

$ b $

解决方案

NSTextView委托方法将让你这样做。你可以只是说不来改变。 ; - )



更新/详细说明(2015年11月)



根据对此答案的评论,我们的想法是在您的文本视图正在编辑。超越,您可以指定自己的属性名称(任何 NSString )和作为该名称的值。



例如,如果您要将一系列文本指定为不可编辑,则可以为该范围添加具有名为(例如) > BOOL 值<$ c $>的> TextIsEditableAttributeName c> YES 或 NO [NSNumber NO] @(NO)(使用ObjC号码拳击 - 相同的结果:一个NSNumber实例)。稍后,当文本视图询问其委托是否应更改范围内的文本时,可以检查 @TextIsEditableAttributeName属性的存在范围。



真的,只需要为不可编辑的范围分配属性,因此您甚至不必检查价值。您可以只在其中放置一个空的 NSData 实例,以便属性具有值。您的属性名称可以是 @EditingLocked或某物。这意味着您只需要在建议范围内的任何位置检查 @EditingLocked属性是否存在当文本视图询问时返回 NO 。这将捕获可编辑范围与不可编辑范围的重叠选择(如果您允许选择复制不可编辑的文本)。



这种方法当然可以用于 -textView:willChangeSelectionFromCharacterRanges:toCharacterRanges:,另一个委托方法允许您返回范围值的已更正数组以供选择。如果您不想允许选择不可编辑的文本,则可以剪切由您的 @EditingLocked属性的任何实例描述的范围



我希望这有助于。


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 NSTextViews 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?

解决方案

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 an NSNumber with a BOOL value of YES or NO: [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 return NO 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.

这篇关于NSTextView文本的不可编辑区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:11