Swift 3 NSAttributedString多个属性

时间:2016-11-24 11:51:24

标签: ios nsattributedstring

刚开始使用swift 3,我遇到了快速语法问题。

我试图显示一个简单的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个属性创建字符串?

7 个答案:

答案 0 :(得分:23)

主要问题是您传递的是数组 [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]

答案 1 :(得分:7)

只需使用两组属性创建一个字典:

let attributes: [String:AnyObject] = 
  [NSFontAttributeName : UIFont.fontSaySomething(), 
  NSForegroundColorAttributeName : UIColor.blue]

然后在创建属性字符串时使用包含键/值对的字典。

Swift中没有用于组合字典的内置机制,但如果您希望能够将字典添加到一起,则可以添加+运算符的覆盖(您可以拥有然而,如果两个词典都包含相同的键,那么可以解决该怎么做。)

答案 2 :(得分:2)

感谢@ 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)

答案 3 :(得分:1)

像这样使用:

let attStringSaySomething = NSAttributedString.init(string: "Hello", attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 16), NSForegroundColorAttributeName:UIColor.black])

答案 4 :(得分:1)

您可以将此代码用于不同字符串上的不同属性使用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字体

答案 5 :(得分:0)

问题是您要将两个词典插入到词典中,只需要[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])

答案 6 :(得分:0)

一些答案​​在这里已经过时,尤其是对于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

因此,最后,您应用了两个属性highlightStringfontHighlightString