2016-11-24 41 views
11

刚刚开始swift 3,我用swift语法有问题。Swift 3 NSAttributedString多个属性

我正试图显示一个简单的NSAttributedString

所以第一次设置我的属性:

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告诉我,我不能,并希望我改变这个字面字典。

如何使用2属性创建我的字符串而不使用NSMutableAttributedString

+0

没有属性必须被包裹成变量? – dirtydanee

+0

不,不,为了更清楚。 – user2206906

回答

18

的主要问题是,你传递一个阵列[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] 
+0

谢谢你的男人。我欣赏。 – user2206906

0

使用这样的:

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

只需创建一个单独的字典,这两套属性:

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

然后用与两个键/值对字典创建您的属性串的时候。

Swift中没有内置机制来结合字典,但是如果你想能够一起添加字典,你可以添加一个覆盖+运算符(如果两本字典都有,你必须弄清楚该做什么但是含有相同的密钥。)

+0

我可以要求检查Swift 4.0版本吗?我第一次尝试使[NSAttributedStringKey:AnyObject]数组,但我不能使用任何NSAttribute ...名称。 – Drwalcore

0

的问题是,要插入两个字典到字典中,只预计[String: Any],而不是[[String: Any], [String: Any]]类型。 你可以做到以下几点:

let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [NSFontAttributeName : UIFont.fontSaySomething(), NSForegroundColorAttributeName : UIColor.blue]) 

你也可以组基于key和value值放入元组,而不是字典,将它们插入到你的字典:

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]) 
+0

谢谢你,我认为你的答案是最好的。 – user2206906

+0

哦,它使用AsyncDisplayKit崩溃 – user2206906

0

您可以使用此用于在不同的字符串不同属性随着的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字体

1

感谢@ vadian的回答

更新对于斯威夫特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)