如何检查NSMutableAttributedString

时间:2016-11-11 01:08:24

标签: objective-c

如果设置了属性NSStrikethroughStyleAttributeName,我想在NSMutableAttributedString上执行一段代码。有没有一种简单的方法来检查是否存在属性?

1 个答案:

答案 0 :(得分:2)

这个怎么样:

NSMutableAttributedString *yourString; //Initialize these things before calling the method below
NSRange range;
if ([yourString attribute:NSStrikethroughStyleAttributeName atIndex:text.length - 1 effectiveRange:&range]) {
    //doSomeStuff
}

文档说它返回“索引处字符名为attributeName的属性的值,如果没有这样的属性,则返回nil”。所以你只需要检查方法是否返回不是nil的东西。

来源:https://developer.apple.com/reference/foundation/nsattributedstring/1408174-attribute?language=objc