ios Swift 3 NSAttributedString 多个属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40785692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 3 NSAttributedString multiple attributes
提问by user2206906
just started swift 3 and I have problems with swift syntax.
刚刚开始使用 swift 3,我在使用 swift 语法时遇到了问题。
i'm trying to display a simple NSAttributedString
.
我正在尝试显示一个简单的NSAttributedString
.
so 1st I set my attributes :
所以第一我设置我的属性:
let attributeFontSaySomething : [String : AnyObject] = [NSFontAttributeName : UIFont.fontSaySomething()]
let attributeColorSaySomething : [String : AnyObject] = [NSForegroundColorAttributeName : UIColor.blue]
Then I create my string :
然后我创建我的字符串:
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: self.attributeFontSaySomething)
What i would like to do is to create the string with my 2 attributes not only just one. But when i do :
我想要做的是用我的 2 个属性创建字符串,而不仅仅是一个。但是当我这样做时:
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [self.attributeFontSaySomething, self.attributeColorSaySomething])
Xcode tells me I can't and want me to change this for a literal dictionary.
Xcode 告诉我我不能,并希望我将其更改为文字字典。
How can I create my string with the 2 attributes without using a NSMutableAttributedString
?
如何在不使用 的情况下创建具有 2 个属性的字符串NSMutableAttributedString
?
回答by vadian
The main issue is that you are passing an array[attr.. , attr...]
rather than one dictionary.
主要问题是您传递的是一个数组[attr.. , attr...]
而不是一个字典。
You need to merge the two dictionaries into one
您需要将两个字典合并为一个
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)
However it might be easier to create the dictionary literally:
但是,从字面上创建字典可能更容易:
let attributes : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 12.0), NSForegroundColorAttributeName : UIColor.blue]
回答by Duncan C
Just create a single dictionary with both sets of attributes:
只需创建一个具有两组属性的字典:
let attributes: [String:AnyObject] =
[NSFontAttributeName : UIFont.fontSaySomething(),
NSForegroundColorAttributeName : UIColor.blue]
And then use the dictionary with both key/value pairs when creating your attributed string.
然后在创建属性字符串时使用带有两个键/值对的字典。
There's no built-in mechanism in Swift for combining dictionaries, but you could add an override of the +
operator if you wanted to be able to add dictionaries together (You'd have to work out what to do if both dictionaries contained the same key however.)
Swift 中没有用于组合字典的内置机制,但是+
如果您希望能够将字典添加在一起,您可以添加运算符的覆盖(但是,如果两个字典都包含相同的键,您必须弄清楚该怎么做) .)
回答by Baljeet Singh
Thanks for @vadian's answer
感谢@vadian 的回答
Update For Swift 4
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)
回答by Poles
Use like this:
像这样使用:
let attStringSaySomething = NSAttributedString.init(string: "Hello", attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 16), NSForegroundColorAttributeName:UIColor.black])
回答by DURGESH
You can use this code for different attributes on different strings With Roboto font (For Roboto fontuse MDFRobotoFontLoader
)
您可以使用此代码对不同字符串的不同属性使用 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)
This example uses Roboto font
此示例使用 Roboto 字体
回答by Umit Kaya
Some of the answers are out dated here, especially for Swift 4 and above, you can use something like:
这里的一些答案已经过时了,尤其是对于 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
So at the end, you apply 2 attributes which are highlightString
and fontHighlightString
所以最后,你应用了 2 个属性,它们是highlightString
和fontHighlightString
回答by dirtydanee
The problem is that you are inserting two dictionaries into a dictionary, what only expects [String: Any]
, not [[String: Any], [String: Any]]
type.
You can do the following:
问题是您将两个字典插入到字典中,只需要什么[String: Any]
,而不是[[String: Any], [String: Any]]
类型。您可以执行以下操作:
let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [NSFontAttributeName : UIFont.fontSaySomething(), NSForegroundColorAttributeName : UIColor.blue])
You could also group the values into tuples instead of dictionaries, and insert them into your dictionary based on the key and 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])