NSAttributedstring 在 swift 中不起作用
NSAttributed string not working in swift
我正在尝试使用属性字符串来自定义标签,但在 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
}
在这两种情况下,都会出现奇怪的错误"Can not invoke 'setAttributes' with an argument list of type '([NSString : ..."
我已经尝试了堆栈溢出和许多其他教程中可用的大部分解决方案,但是所有这些都导致了此类错误。
你的问题是你传递了一个 swift Range
而预期 NSRange
。
从字符串中获取有效 NSRange
的解决方案是先将其转换为 NSString
。参见 NSAttributedString takes an NSRange while I'm using a Swift String that uses Range。
所以像这样的东西应该可以工作:
let nsText = self.text as NSString
let theRange = nsText.rangeOfString(substring) // this is a NSRange, not Range
// ... snip ...
attributedString.setAttributes(attribute, range: theRange)
尝试使用 NSRange 而不是 Range:
func redBlackSubstring(substring: String) {
self.font = UIFont(name: "HelveticaNeue", size: 12.0)!
var range: NSRange = (self.text as NSString).rangeOfString(substring)
var attributedString = NSMutableAttributedString(string: self.text)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: range)
self.attributedText = attributedString
}
罪魁祸首是Range。使用 NSRange 而不是范围。这里要注意的另一件事是,简单地将 self.text 转换为 NSString 会给你强制解包的错误。
因此,请改用 "self.text! as NSString"。
func redBlackSubstring(substring: String) {
self.font = UIFont(name: "HelveticaNeue", size: 12.0)!
var range: NSRange = (self.text! as NSString).rangeOfString(substring)
var attributedString = NSMutableAttributedString(string: self.text)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: range)
self.attributedText = attributedString
}
我正在尝试使用属性字符串来自定义标签,但在 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
}
在这两种情况下,都会出现奇怪的错误"Can not invoke 'setAttributes' with an argument list of type '([NSString : ..."
我已经尝试了堆栈溢出和许多其他教程中可用的大部分解决方案,但是所有这些都导致了此类错误。
你的问题是你传递了一个 swift Range
而预期 NSRange
。
从字符串中获取有效 NSRange
的解决方案是先将其转换为 NSString
。参见 NSAttributedString takes an NSRange while I'm using a Swift String that uses Range。
所以像这样的东西应该可以工作:
let nsText = self.text as NSString
let theRange = nsText.rangeOfString(substring) // this is a NSRange, not Range
// ... snip ...
attributedString.setAttributes(attribute, range: theRange)
尝试使用 NSRange 而不是 Range:
func redBlackSubstring(substring: String) {
self.font = UIFont(name: "HelveticaNeue", size: 12.0)!
var range: NSRange = (self.text as NSString).rangeOfString(substring)
var attributedString = NSMutableAttributedString(string: self.text)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: range)
self.attributedText = attributedString
}
罪魁祸首是Range。使用 NSRange 而不是范围。这里要注意的另一件事是,简单地将 self.text 转换为 NSString 会给你强制解包的错误。
因此,请改用 "self.text! as NSString"。
func redBlackSubstring(substring: String) {
self.font = UIFont(name: "HelveticaNeue", size: 12.0)!
var range: NSRange = (self.text! as NSString).rangeOfString(substring)
var attributedString = NSMutableAttributedString(string: self.text)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: range)
self.attributedText = attributedString
}