本文介绍了有效范围和最长有效范围之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSAttributedString类中,有一些函数可以获取某些索引和范围的属性值,但我不确定选择器attributesAtIndex:effectiveRangeattributesAtIndex:longestEffectiveRange:inRange:之间的区别,以及何时使用一个代替而不是另一个?预先感谢您的澄清

In the NSAttributedString class, there are functions that get the values of attributes for certain indices and ranges but I'm unsure of the difference between the selectors attributesAtIndex:effectiveRange and attributesAtIndex:longestEffectiveRange:inRange: and when would I use one instead of the other? Thanks in advance for any clarification

推荐答案

假设您具有这种属性:
范围[0,2]:黑色背景色
范围[0,2]:粗体
范围[2,4]:黑色背景色
范围[2,4]:斜体字体

Let's say you have this kind of attributes:
Range [0,2]: Black Background Color
Range [0,2]: Bold Font
Range [2,4]: Black Background Color
Range [2,4]: Italic Font

如果您记录此NSAttributedString,则其属性将分为两部分:
范围[0,2]:黑色背景色和粗体字体
范围[2,4]:黑色背景色和斜体字体
这些属性的作用类似于NSDictionary,它们在两个范围内有2个不同的地方.

If you log this NSAttributedString, its attributes will be separated in two:
Range [0,2]: Black Background Color AND Bold Font
Range [2,4]: Black Background Color AND Italic Font
These attributes works like a NSDictionary, they are 2 different at two ranges.

如果在NSBackgroundColorAttributeName的索引0处使用longestEffectiveRange,则会得到[0,4]的范围.
如果使用effectiveRange,则会得到[0,2].
换句话说,longestEffectiveRange将检查下一个索引(在effectiveRange结束之后)是否存在另一个可能具有属性字典的索引(如果它与该索引共享所需的值,然后附加范围).

If you use longestEffectiveRange at index 0 for NSBackgroundColorAttributeName, you'll get a range of [0,4].
If you use effectiveRange, you'll get [0,2].
In other words, the longestEffectiveRange will check if there is on the next index (after the end of effectiveRange) that may have another dictionary of attributes if it shares the wanted value with it and then append the range.

示例代码:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"BoBiBu"];

[attributedString addAttribute:NSBackgroundColorAttributeName
                         value:[UIColor blackColor]
                         range:NSMakeRange(0, 2)];
[attributedString addAttribute:NSFontAttributeName
                         value:[UIFont boldSystemFontOfSize:12]
                         range:NSMakeRange(0, 2)];
[attributedString addAttribute:NSFontAttributeName
                         value:[UIFont italicSystemFontOfSize:12]
                         range:NSMakeRange(2, 2)];
[attributedString addAttribute:NSBackgroundColorAttributeName
                         value:[UIColor blackColor]
                         range:NSMakeRange(2, 2)];
NSLog(@"AttributedString: %@", attributedString);


NSRange range1;
NSLog(@"Attribute: %@", [attributedString attribute:NSBackgroundColorAttributeName atIndex:0 effectiveRange:&range1]);
NSLog(@"EffectiveRange of previous attribute: %@", NSStringFromRange(range1));

NSRange range2;
NSLog(@"Attribute: %@", [attributedString attribute:NSBackgroundColorAttributeName atIndex:0 longestEffectiveRange:&range2 inRange:NSMakeRange(0, 6)]);
NSLog(@"Longest effective Range of previous attribute: %@", NSStringFromRange(range2));

日志:

> AttributedString: Bo{
    NSBackgroundColor = "UIDeviceWhiteColorSpace 0 1";
    NSFont = "<UICTFont: 0x7fd3e2f80ec0> font-family: \".HelveticaNeueInterface-MediumP4\"; font-weight: bold; font-style: normal; font-size: 12.00pt";
}Bi{
    NSBackgroundColor = "UIDeviceWhiteColorSpace 0 1";
    NSFont = "<UICTFont: 0x7fd3e2f83310> font-family: \".HelveticaNeueInterface-Italic\"; font-weight: normal; font-style: italic; font-size: 12.00pt";
}Bu{
}
> Attribute: UIDeviceWhiteColorSpace 0 1
> EffectiveRange of previous attribute: {0, 2}
> Attribute: UIDeviceWhiteColorSpace 0 1
> Longest effective Range of previous attribute: {0, 4}

这篇关于有效范围和最长有效范围之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:38