iOS 6之前是否有NSAttributedString可用?

9
我发现有关iOS 6之前版本是否可用NSAttributedString的信息存在冲突。如果它具有向后兼容性,那么它将非常有用。
这里说它从iOS 4开始就可用: 为什么iPhone上没有NSAttributedString? 在苹果网站上,它说是iOS 6及更高版本: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/Reference/Reference.html 我尝试像这样在我的代码中实现:
NSAttributedString *commentString = [[NSAttributedString alloc] initWithString:@"I really love raisins" attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];

它可以在iOS 6中工作,但是在尝试在iOS 5或iOS 5.1中运行时无法工作。我没有测试早期版本。 我只是试图使用较旧版本中不包含的功能吗?
3个回答

14

自iOS3.2以来,它一直存在。然而,仅从iOS6开始才能在某些UI控件(如UILabel、UIButton、UITextView等)中使用。对于< 6.0,您仍可以使用CoreText。


6

NSAttributeStringinitWithString:attributes:方法自iOS 3.2版本起就存在于iOS参考文档中。一些NSAttributedString的方法是在iOS 4.0中加入的。所有添加为UIKit类别的方法都是在iOS 6.0中添加的。

这行代码存在的问题是:

NSAttributedString *commentString = [[NSAttributedString alloc] initWithString:@"I really love raisins" attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];

问题在于,直到iOS 6.0才添加了NSForegroundColorAttributeName常量。

因此,虽然NSAttributedString已经存在一段时间,但所有有用的东西都是在6.0之后添加的。


1
这是两天内我们在几秒钟内回答了一个iOS问题,致敬。 - Bergasms

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接