当NSAttributedString属性的范围嵌套/重叠时,它们是否会堆叠?

时间:2014-01-24 21:27:51

标签: ios uikit nsattributedstring

我正在将简单的HTML解析为属性字符串(对于iOS 6),我似乎无法获得嵌套属性。例如:

This <font color="red">is <i>what</i> I mean</font> to achieve.

我正确应用属性,但斜体永远不会生效,除非它是在字体颜色不影响的范围内完成的。我的谷歌搜索没有提出任何相关问题,我想知道NSAttributedString是否甚至支持这一点,或者属性范围是否必须不重叠。

编辑:

我把这个问题改写得更清楚了。问题的答案是响亮的NO,您必须在单个属性字典中指定给定范围的所有属性,它们不会合并。我已经接受了对原始问题的合理答案是正确的。

2 个答案:

答案 0 :(得分:1)

您可以使用NSMutableAttributedString获得所需的结果。您只需要单独设置每个属性。 E.g。

NSString *string = @"This is a test";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string attributes:nil];

NSMutableDictionary *attributes = [@{NSForegroundColorAttributeName: [UIColor redColor]} mutableCopy];

[string setAttributes:attributes range:[string rangeOfString:@"is a test"];

[attributes setObject:font forKey:NSFontAttributeName];

[string setAttributes:attributes range:[string rangeOfString:@"test"];

答案 1 :(得分:1)

是的,您可以拥有特定范围的特定属性。尽可能多的人。