Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSAttributedString属性的范围嵌套/重叠时是否堆叠?_Ios_Uikit_Nsattributedstring - Fatal编程技术网

Ios NSAttributedString属性的范围嵌套/重叠时是否堆叠?

Ios NSAttributedString属性的范围嵌套/重叠时是否堆叠?,ios,uikit,nsattributedstring,Ios,Uikit,Nsattributedstring,我正在将简单的HTML解析为一个属性字符串(对于iOS 6),而我似乎无法让嵌套的属性正常工作。例如: This <font color="red">is <i>what</i> I mean</font> to achieve. 这就是我想要实现的目标。 我正确地应用了属性,但是斜体化永远不会生效,除非它是在字体颜色不影响的范围内进行的。我的谷歌搜索没有提出任何相关的问题,我想知道NSAttributedString是否支持这一点,或者属性范

我正在将简单的HTML解析为一个属性字符串(对于iOS 6),而我似乎无法让嵌套的属性正常工作。例如:

This <font color="red">is <i>what</i> I mean</font> to achieve.
这就是我想要实现的目标。
我正确地应用了属性,但是斜体化永远不会生效,除非它是在字体颜色不影响的范围内进行的。我的谷歌搜索没有提出任何相关的问题,我想知道NSAttributedString是否支持这一点,或者属性范围是否必须不重叠

编辑:


我把问题改写得更清楚了。这个问题的答案是响亮的“否”,您必须在单个属性字典中指定给定范围的所有属性,它们不能组合。我接受了对原始问题的合理回答,认为是正确的。

您可以使用
NSMutableAttributedString
实现您想要的结果。您只需分别设置每个属性。例如

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"];

是的,您可以具有特定范围的特定属性。你想要多少就有多少。

我认为你需要把它分成不同的范围。标记
为红色。Msrk
what
为红色和斜体。将“我的意思是”标记为红色。尝试使用可以有斜体版本的字体。谢谢rmaddy,这是我正在寻求的解决方案。显示重叠属性。这不会产生我想要的效果。字体属性覆盖颜色属性,我希望它们堆叠。谢谢,我想这意味着属性不嵌套。你是对的,我用正确的方法编辑了我的答案。
addAttribute
应该更好,所以这是实现我的示例的正确方法,但问题的措辞很糟糕。我的意思是问属性是否在其范围重叠/嵌套时堆叠,答案是否,您必须按照本文所述操作,并使用其所有属性指定每个范围。