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

问题描述

NSAttributedString 类中,有一些函数可以获取某些索引和范围的属性值,但我不确定选择器 attributesAtIndex:effectiveRange 之间的区别和 attributesAtIndex: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.

如果您在索引 0 处为 NSBackgroundColorAttributeName 使用 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}

这篇关于EffectiveRange 和 LongestEffectiveRange 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 14:06