Ios Swift 3 NSAttribute字符串多属性
刚开始使用swift 3,我对swift语法有问题 我试图显示一个简单的Ios Swift 3 NSAttribute字符串多属性,ios,nsattributedstring,Ios,Nsattributedstring,刚开始使用swift 3,我对swift语法有问题 我试图显示一个简单的nsattributed字符串 因此,我首先设置我的属性: let attributeFontSaySomething : [String : AnyObject] = [NSFontAttributeName : UIFont.fontSaySomething()] let attributeColorSaySomething : [String : AnyObject] = [NSForegroundColorAttri
nsattributed字符串
因此,我首先设置我的属性:
let attributeFontSaySomething : [String : AnyObject] = [NSFontAttributeName : UIFont.fontSaySomething()]
let attributeColorSaySomething : [String : AnyObject] = [NSForegroundColorAttributeName : UIColor.blue]
然后我创建我的字符串:
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: self.attributeFontSaySomething)
我想做的是用我的两个属性创建字符串,而不仅仅是一个属性。但当我这样做的时候:
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [self.attributeFontSaySomething, self.attributeColorSaySomething])
Xcode告诉我不能这样做,并希望我将其更改为文字词典
如何在不使用NSMutableAttributedString
的情况下创建具有这两个属性的字符串 像这样使用:
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]
然后在创建属性字符串时,将字典与两个键/值对一起使用
Swift中没有用于组合字典的内置机制,但是如果希望能够将字典添加到一起,可以添加对
+
操作符的覆盖(但是,如果两个字典包含相同的键,则必须确定该怎么做)主要问题是您传递的是一个数组[attr..,attr…]
,而不是一个字典
你需要把这两本词典合并成一本
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
)
本例使用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
最后,你应用了两个属性,分别是
highlightString
和fontHighlightString
属性是否必须包装到变量中?不,不,只是为了更清楚。哦,它使用AsyncDisplayKit崩溃了。我可以要求检查Swift 4.0版本吗?我首先尝试创建[NSAttributedStringKey:AnyObject]数组,但我无法使用任何NSAttribute…名称。您正在获取一个NSAttributedStringKey
,将其转换为字符串
,然后使用该字符串
构造一个NSAttributedStringKey
。根本没有这个必要。只需使用NSAttributedStringKey
。实际上,您可以使用.font
而不是NSAttributedStringKey.font
。
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