【发布时间】:2015-06-02 20:19:56
【问题描述】:
我正在尝试使用属性字符串来自定义标签,但在 swift 中出现奇怪的错误。
func redBlackSubstring(substring: String) {
self.font = UIFont(name: "HelveticaNeue", size: 12.0)
var theRange: Range<String.Index>! = self.text?.rangeOfString(substring)
var attributedString = NSMutableAttributedString(string: self.text!)
let attribute = [NSForegroundColorAttributeName as NSString: UIColor.blackColor()]
attributedString.setAttributes(attribute, range: self.text?.rangeOfString(substring))
self.attributedText = attributedString
}
我也尝试过使用下面的代码
func redBlackSubstring(substring: String) {
self.font = UIFont(name: "HelveticaNeue", size: 12.0)
var theRange: Range<String.Index>! = self.text?.rangeOfString(substring)
var attributedString = NSMutableAttributedString(string: self.text!)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: self.text?.rangeOfString(substring))
self.attributedText = attributedString
}
在这两种情况下,都会出现奇怪的错误“无法使用 '([NSString : ..." 类型的参数列表调用 'setAttributes'”
我已经尝试了堆栈溢出和许多其他教程中可用的大多数解决方案,但是所有这些都导致了此类错误。
【问题讨论】:
-
遇到了同样的问题。还是没有找到解决办法。感谢您的提问。
标签: ios swift nsattributedstring