Swift 3 NSAttributedString 多个属性
Swift 3 NSAttributedString multiple attributes
刚开始 swift 3,我对 swift 语法有疑问。
我正在尝试显示一个简单的 NSAttributedString
。
所以我首先设置我的属性:
let attributeFontSaySomething : [String : AnyObject] = [NSFontAttributeName : UIFont.fontSaySomething()]
let attributeColorSaySomething : [String : AnyObject] = [NSForegroundColorAttributeName : UIColor.blue]
然后我创建我的字符串:
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: self.attributeFontSaySomething)
我想做的是用我的 2 个属性创建字符串,而不仅仅是一个。但是当我这样做时:
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [self.attributeFontSaySomething, self.attributeColorSaySomething])
Xcode 告诉我我不能,并希望我将其更改为文字词典。
如何在不使用 NSMutableAttributedString
的情况下创建具有 2 个属性的字符串?
这样使用:
let attStringSaySomething = NSAttributedString.init(string: "Hello", attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 16), NSForegroundColorAttributeName:UIColor.black])
只需创建一个包含两组属性的字典:
let attributes: [String:AnyObject] =
[NSFontAttributeName : UIFont.fontSaySomething(),
NSForegroundColorAttributeName : UIColor.blue]
然后在创建属性字符串时使用包含两个 key/value 对的字典。
Swift 中没有用于组合字典的内置机制,但如果您希望能够将字典添加到一起,则可以添加对 +
运算符的覆盖(您需要计算出如果两个词典包含相同的键该怎么办。)
主要问题是您传递的是 array [attr.. , attr...]
而不是 dictionary.
您需要将两个词典合并为一个
let attributeFontSaySomething : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 12.0)]
let attributeColorSaySomething : [String : Any] = [NSForegroundColorAttributeName : UIColor.blue]
var attributes = attributeFontSaySomething
for (key, value) in attributeColorSaySomething {
attributes(value, forKey: key)
}
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: attributes)
然而,按字面意思创建字典可能更容易:
let attributes : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 12.0), NSForegroundColorAttributeName : UIColor.blue]
问题是您将两个字典插入到一个字典中,只需要 [String: Any]
,而不是 [[String: Any], [String: Any]]
类型。
您可以执行以下操作:
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [NSFontAttributeName : UIFont.fontSaySomething(), NSForegroundColorAttributeName : UIColor.blue])
您也可以将值分组为元组而不是字典,然后根据键和值将它们插入到您的字典中:
let attributeFontSaySomething = (key: NSFontAttributeName, value: UIFont.fontSaySomething())
let attributeColorSaySomething = (key: NSForegroundColorAttributeName, value: UIColor.blue)
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [attributeFontSaySomething.key : attributeFontSaySomething.value,attributeColorSaySomething.key : attributeColorSaySomething.value])
您可以将此代码用于不同字符串的不同属性使用 Roboto 字体(对于 Roboto 字体 使用 MDFRobotoFontLoader
)
let yourAttributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: MDFRobotoFontLoader.sharedInstance().regularFont(ofSize: 20)]
let finalString = NSMutableAttributedString(string: "", attributes: yourAttributes)
let attributeStr = NSMutableAttributedString(string: "XYZGFDGii", attributes: yourAttributes)
finalString.append(attributeStr)
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: MDFRobotoFontLoader.sharedInstance().regularFont(ofSize: 24)]
let partTwo = NSMutableAttributedString(string: "hfjghlkdhkjld", attributes: yourOtherAttributes)
finalString.append(partTwo)
本例使用 Roboto 字体
感谢@vadian 的回答
更新 Swift 4
let attributes : [NSAttributedStringKey : Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue) : UIFont.systemFont(ofSize: 12.0), NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue) : UIColor(hex:"4C0000")]
refreshControl.attributedTitle=NSAttributedString(string: "Refreshing...", attributes: attributes)
有些答案在这里已经过时了,特别是 Swift 4 及以上,你可以使用类似的东西:
let wholeString = "This is whole string"
let partToAttribute = "whole string"
let attributedMessage = NSMutableAttributedString(string: wholeString)
.highlightString(with: UIColor.blue, for: partToAttribute, isBackground: true)
.fontHighlightString(with: UIFont.makeBoldFont(size: 16), color: UIColor.white, for: partToAttribute)
titleLabel.attributedText = attributedMessage
所以最后,您应用了 2 个属性,它们是 highlightString
和 fontHighlightString
首先你可以使用
初始化属性
var myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.init(hexString: "#FFAEA9"), NSAttributedString.Key.font: UIFont(name: "Dubai-Medium", size: 16) ]
之后就可以使用了...
let myString = "Enter default amount"
let text = NSAttributedString(string: myString, attributes: myAttribute)
enterCustomAmount.setAttributedTitle(text, for: .normal)
刚开始 swift 3,我对 swift 语法有疑问。
我正在尝试显示一个简单的 NSAttributedString
。
所以我首先设置我的属性:
let attributeFontSaySomething : [String : AnyObject] = [NSFontAttributeName : UIFont.fontSaySomething()]
let attributeColorSaySomething : [String : AnyObject] = [NSForegroundColorAttributeName : UIColor.blue]
然后我创建我的字符串:
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: self.attributeFontSaySomething)
我想做的是用我的 2 个属性创建字符串,而不仅仅是一个。但是当我这样做时:
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [self.attributeFontSaySomething, self.attributeColorSaySomething])
Xcode 告诉我我不能,并希望我将其更改为文字词典。
如何在不使用 NSMutableAttributedString
的情况下创建具有 2 个属性的字符串?
这样使用:
let attStringSaySomething = NSAttributedString.init(string: "Hello", attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 16), NSForegroundColorAttributeName:UIColor.black])
只需创建一个包含两组属性的字典:
let attributes: [String:AnyObject] =
[NSFontAttributeName : UIFont.fontSaySomething(),
NSForegroundColorAttributeName : UIColor.blue]
然后在创建属性字符串时使用包含两个 key/value 对的字典。
Swift 中没有用于组合字典的内置机制,但如果您希望能够将字典添加到一起,则可以添加对 +
运算符的覆盖(您需要计算出如果两个词典包含相同的键该怎么办。)
主要问题是您传递的是 array [attr.. , attr...]
而不是 dictionary.
您需要将两个词典合并为一个
let attributeFontSaySomething : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 12.0)]
let attributeColorSaySomething : [String : Any] = [NSForegroundColorAttributeName : UIColor.blue]
var attributes = attributeFontSaySomething
for (key, value) in attributeColorSaySomething {
attributes(value, forKey: key)
}
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: attributes)
然而,按字面意思创建字典可能更容易:
let attributes : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 12.0), NSForegroundColorAttributeName : UIColor.blue]
问题是您将两个字典插入到一个字典中,只需要 [String: Any]
,而不是 [[String: Any], [String: Any]]
类型。
您可以执行以下操作:
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [NSFontAttributeName : UIFont.fontSaySomething(), NSForegroundColorAttributeName : UIColor.blue])
您也可以将值分组为元组而不是字典,然后根据键和值将它们插入到您的字典中:
let attributeFontSaySomething = (key: NSFontAttributeName, value: UIFont.fontSaySomething())
let attributeColorSaySomething = (key: NSForegroundColorAttributeName, value: UIColor.blue)
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [attributeFontSaySomething.key : attributeFontSaySomething.value,attributeColorSaySomething.key : attributeColorSaySomething.value])
您可以将此代码用于不同字符串的不同属性使用 Roboto 字体(对于 Roboto 字体 使用 MDFRobotoFontLoader
)
let yourAttributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: MDFRobotoFontLoader.sharedInstance().regularFont(ofSize: 20)]
let finalString = NSMutableAttributedString(string: "", attributes: yourAttributes)
let attributeStr = NSMutableAttributedString(string: "XYZGFDGii", attributes: yourAttributes)
finalString.append(attributeStr)
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: MDFRobotoFontLoader.sharedInstance().regularFont(ofSize: 24)]
let partTwo = NSMutableAttributedString(string: "hfjghlkdhkjld", attributes: yourOtherAttributes)
finalString.append(partTwo)
本例使用 Roboto 字体
感谢@vadian 的回答
更新 Swift 4
let attributes : [NSAttributedStringKey : Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue) : UIFont.systemFont(ofSize: 12.0), NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue) : UIColor(hex:"4C0000")]
refreshControl.attributedTitle=NSAttributedString(string: "Refreshing...", attributes: attributes)
有些答案在这里已经过时了,特别是 Swift 4 及以上,你可以使用类似的东西:
let wholeString = "This is whole string"
let partToAttribute = "whole string"
let attributedMessage = NSMutableAttributedString(string: wholeString)
.highlightString(with: UIColor.blue, for: partToAttribute, isBackground: true)
.fontHighlightString(with: UIFont.makeBoldFont(size: 16), color: UIColor.white, for: partToAttribute)
titleLabel.attributedText = attributedMessage
所以最后,您应用了 2 个属性,它们是 highlightString
和 fontHighlightString
首先你可以使用
初始化属性var myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.init(hexString: "#FFAEA9"), NSAttributedString.Key.font: UIFont(name: "Dubai-Medium", size: 16) ]
之后就可以使用了...
let myString = "Enter default amount"
let text = NSAttributedString(string: myString, attributes: myAttribute)
enterCustomAmount.setAttributedTitle(text, for: .normal)