An attributed string identifies attributes by name, storing a value under the attribute name in an NSDictionary
object, which is in turn associated with an NSRange
that indicates the characters to which the dictionary’s attributes apply. You can assign any attribute name-value pair you wish to a range of characters, in addition to the standard attributes.
- 属性字符串按名称标识属性,在NSDictionary对象中的属性名称下存储值,NSDictionary对象又与NSRange关联,NSRange指示字典属性所适用的字符。 除标准属性外,您还可以将任何属性名称 - 值对分配给一系列字符。
Retrieving Attribute Values
- 检索属性值
With an immutable attributed string, you assign all attributes when you create the string. In Java, you use the constructors. In Objective-C, you use methods such as initWithString:attributes:
, which explicitly take an NSDictionary
object of name-value pairs, or initWithString:
, which assigns no attributes. And the Application Kit’s extensions to NSAttributedString
adds methods that take an RTF file or an HTML file. See Changing an Attributed String for information on assigning attributes with a mutable attributed string.
- 使用不可变的属性字符串,可以在创建字符串时分配所有属性。 在Java中,您使用构造函数。 在Objective-C中,您使用诸如initWithString:attributes:之类的方法,它显式地采用名称 - 值对的NSDictionary对象,或initWithString:,它不分配任何属性。 Application Kit对NSAttributedString的扩展添加了采用RTF文件或HTML文件的方法。 有关使用可变属性字符串分配属性的信息,请参阅更改属性字符串。
To retrieve attribute values from either type of attributed string, use any of these methods:
要从任一类型的属性字符串中检索属性值,请使用以下任一方法:
-
attributesAtIndex:effectiveRange:
-
attributesAtIndex:longestEffectiveRange:inRange:
-
attribute:atIndex:effectiveRange:
-
attribute:atIndex:longestEffectiveRange:inRange:
-
fontAttributesInRange:
-
rulerAttributesInRange:
The first two methods return all attributes at a given index, the attribute:...
methods return the value of a single named attribute. The Application Kit’s extensions to NSAttributedString
add fontAttributesInRange:
and rulerAttributesInRange:
, which return attributes defined to apply only to characters or to whole paragraphs, respectively.
- 前两个方法返回给定索引处的所有属性,属性:... methods返回单个命名属性的值。 Application Kit对NSAttributedString的扩展添加了fontAttributesInRange:和rulerAttributesInRange :,它们返回的属性分别仅适用于字符或整个段落。
The first four methods also return by reference the effective range and the longest effective range of the attributes. These ranges allow you to determine the extent of attributes. Conceptually, each character in an attributed string has its own collection of attributes; however, it’s often useful to know when the attributes and values are the same over a series of characters. This allows a routine to progress through an attributed string in chunks larger than a single character. In retrieving the effective range, an attributed string simply looks up information in its attribute mapping, essentially the dictionary of attributes that apply at the index requested. In retrieving the longest effective range, the attributed string continues checking characters past this basic range as long as the attribute values are the same. This extra comparison increases the execution time for these methods but guarantees a precise maximal range for the attributes requested.
- 前四种方法也通过引用返回属性的有效范围和最长有效范围。 这些范围允许您确定属性的范围。 从概念上讲,属性字符串中的每个字符都有自己的属性集合; 但是,了解一系列字符的属性和值何时相同通常很有用。 这允许例程以大于单个字符的块中的属性字符串进行处理。 在检索有效范围时,属性字符串只是在其属性映射中查找信息,实质上是在请求的索引处应用的属性字典。 在检索最长有效范围时,只要属性值相同,属性字符串就会继续检查超过此基本范围的字符。 这种额外的比较增加了这些方法的执行时间,但保证了所请求属性的精确最大范围。
Effective and Maximal Ranges
Methods that return an effective range by reference are not guaranteed to return the maximal range to which the attribute(s) apply; they are merely guaranteed to return some range over which they apply. In practice they will return whatever range is readily available from the attributed string's internal storage mechanisms, which may depend on the implementation and on the precise history of modifications to the attributed string.
- 通过引用返回有效范围的方法不保证返回属性适用的最大范围; 它们只能保证返回它们适用的范围。 实际上,它们将返回属性字符串的内部存储机制中可用的任何范围,这可能取决于实现以及对属性字符串的修改的精确历史记录。
Methods that return a longest effective range by reference, on the other hand, are guaranteed to return the longest range containing the specified index to which the attribute(s) in question apply (constrained by the value of the argument passed in for inRange:
). For efficiency, it is important that the inRange:
argument should be as small as appropriate for the range of interest to the client.
- 另一方面,通过引用返回最长有效范围的方法保证返回包含所讨论的属性所适用的指定索引的最长范围(受inRange传入的参数值约束:)。 为了提高效率,重要的是inRange:参数应尽可能小到客户感兴趣的范围。
When you iterate over an attributed string by attribute ranges, either sort of method may be appropriate depending on the situation. If there is some processing to be done for each range, and you know that the full range for a given attribute is going to have to be handled eventually, it may be more efficient to use the longest-effective-range variant, so as not to have to handle the range in pieces. However, you should use the longest-effective-range methods with caution, because the longest effective range could be quite long—potentially the entire length of the document, if the inRange:
argument is not constrained.
- 当您按属性范围迭代属性字符串时,根据情况,任何一种方法都可能是合适的。 如果要为每个范围进行一些处理,并且您知道最终必须处理给定属性的完整范围,则使用最长有效范围变量可能更有效,因此 必须处理片断范围。 但是,您应该谨慎使用最长有效范围的方法,因为如果inRange:参数不受约束,则最长有效范围可能非常长 - 可能是文档的整个长度。
The Objective-C code fragment below progresses through an attributed string in chunks based on the effective range. The fictitious analyzer object here counts the number of characters in each font. The while loop progresses as long as the effective range retrieved does not include the end of the attributed string, retrieving the font in effect just past the latest retrieved range. For each font attribute retrieved, the analyzer tallies the number of characters in the effective range. In this example, it is possible that consecutive invocations of attribute:atIndex:effectiveRange:
will return the same value.
- 下面的Objective-C代码片段基于有效范围以块的形式前进到属性字符串。 这里虚构的分析器对象计算每种字体中的字符数。 只要检索到的有效范围不包括属性字符串的结尾,检索刚刚超过最新检索范围的字体,while循环就会进行。 对于检索到的每个字体属性,分析器会计算有效范围内的字符数。 在此示例中,属性:atIndex:effectiveRange:的连续调用可能会返回相同的值。
NSAttributedString *attrStr;
unsigned int length;
NSRange effectiveRange;
id attributeValue;
length = [attrStr length];
effectiveRange = NSMakeRange(0, 0);
while (NSMaxRange(effectiveRange) < length) {
attributeValue = [attrStr attribute:NSFontAttributeName
atIndex:NSMaxRange(effectiveRange) effectiveRange:&effectiveRange];
[analyzer tallyCharacterRange:effectiveRange font:attributeValue];
}
In contrast, the next Objective-C code fragment progresses through the attributed string according to the maximum effective range for each font. In this case, the analyzer counts font changes, which may not be represented by merely retrieving effective ranges. In this case the while loop is predicated on the length of the limiting range, which begins as the entire length of the attributed string and is whittled down as the loop progresses. After the analyzer records the font change, the limit range is adjusted to account for the longest effective range retrieved.
- 相反,下一个Objective-C代码片段根据每种字体的最大有效范围通过属性字符串进行。 在这种情况下,分析器会计算字体更改,这可能无法仅通过检索有效范围来表示。 在这种情况下,while循环基于限制范围的长度,该限制范围的长度从属性字符串的整个长度开始并随着循环的进展而减弱。 在分析仪记录字体更改后,将调整限制范围以考虑检索到的最长有效范围。
NSAttributedString *attrStr;
NSRange limitRange;
NSRange effectiveRange;
id attributeValue;
limitRange = NSMakeRange(0, [attrStr length]);
while (limitRange.length > 0) {
attributeValue = [attrStr attribute:NSFontAttributeName
atIndex:limitRange.location longestEffectiveRange:&effectiveRange
inRange:limitRange];
[analyzer recordFontChange:attributeValue];
limitRange = NSMakeRange(NSMaxRange(effectiveRange),
NSMaxRange(limitRange) - NSMaxRange(effectiveRange));
}
Note that the second code fragment is more complex. Because of this, and because attribute:atIndex:longestEffectiveRange:inRange: is somewhat slower than attribute:atIndex:effectiveRange:, you should typically use it only when absolutely necessary for the work you’re performing. In most cases working by effective range is enough.
- 请注意,第二个代码片段更复杂。 因此,并且因为属性:atIndex:longestEffectiveRange:inRange:比属性:atIndex:effectiveRange:慢一些,通常只有在你正在执行的工作绝对必要时才使用它。 在大多数情况下,按有效范围工作就足够了。
网友评论