05. Changing an Attributed Strin

2018-10-29  本文已影响0人  ngugg

相关链接:
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/ChangingAttrStrings.html#//apple_ref/doc/uid/20000162-BBCBGCDG

NSMutableAttributedString declares a number of methods for changing both characters and attributes. You must take care not to modify attribute values after they have been passed to an attributed string. You may also need to repair inconsistencies that can be introduced if you modify an attributed string.

Modifying Attributes

NSMutableAttributedString declares a number of methods for changing both characters and attributes, such as the primitive replaceCharactersInRange:withString: and setAttributes:range:, or the more convenient methods addAttribute:value:range:, applyFontTraits:range:, and so on.

The following example illustrates how to specify a link attribute for a selected range in an attributed string, underline the text, and color it blue. Note that you can define whatever value you want for the link attribute, it is up to you to interpret the value when the link is selected—see Accessing Attributes—typically, however, you use either a string or a URL. For an explanation of the role of beginEditing and endEditing (shown in the sample), see Fixing Inconsistencies.

NSMutableAttributedString *string; // assume string exists
NSRange selectedRange; // assume this is set
 
NSURL *linkURL = [NSURL URLWithString:@"http://www.apple.com/"];
 
[string beginEditing];
[string addAttribute:NSLinkAttributeName
               value:linkURL
               range:selectedRange];
 
[string addAttribute:NSForegroundColorAttributeName
               value:[NSColor blueColor]
               range:selectedRange];
 
[string addAttribute:NSUnderlineStyleAttributeName
               value:[NSNumber numberWithInt:NSSingleUnderlineStyle]
               range:selectedRange];
[string endEditing];

Attribute values assigned to an attributed string become the property of that string, and should not be modified “behind the attributed string” by other objects. Doing so can render inconsistent the attributed string’s internal state. There are two main reasons for this:
分配给属性字符串的属性值将成为该字符串的属性,不应被其他对象“隐藏在属性字符串后面”。 这样做会导致属性字符串的内部状态不一致。 这有两个主要原因:

If you must change attribute values, and are sure that the change will apply to the correct range, there are two strategies you can adopt:
如果必须更改属性值,并确保更改将应用于正确的范围,则可以采用两种策略:

Fixing Inconsistencies

All of the methods for changing a mutable attributed string properly update the mapping between characters and attributes, but after a change some inconsistencies can develop. Here are some examples of attribute consistency requirements:
所有用于更改可变属性字符串的方法都会正确地更新字符和属性之间的映射,但在更改之后,可能会出现一些不一致的情况。 以下是属性一致性要求的一些示例:

The Application Kit’s extensions to NSMutableAttributedString define methods to fix these inconsistencies as changes are made. This allows the attributes to be cleaned up at a low level, hiding potential problems from higher levels and providing for very clean update of display as attributes change. There are four methods for fixing attributes and two to group editing changes:
Application Kit对NSMutableAttributedString的扩展定义了在进行更改时修复这些不一致的方法。 这允许在较低级别清理属性,从较高级别隐藏潜在问题,并在属性改变时提供非常干净的显示更新。 有四种方法可以修复属性,两种方法可以对编辑更改进行分组:

The first method, fixAttributesInRange:, invokes the other three fix... methods to clean up deleted attachment references, font attributes, and paragraph attributes, respectively. The individual method descriptions explain what cleanup entails for each case.

NSMutableAttributedString provides beginEditing and endEditing methods for subclasses of NSMutableAttributedString to override. These methods allow instances of a subclass to record or buffer groups of changes and clean themselves up on receiving an endEditing message. The endEditing method also allows the receiver to notify any observers that it has been changed. NSTextStorage’s implementation of endEditing, for example, fixes changed attributes and then notifies its layout managers that they need to re-lay and redisplay their text. The default implementations do nothing.

上一篇 下一篇

猜你喜欢

热点阅读