ios 如何在 NSAttributedString 上设置字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45959903/
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
How to set font size on NSAttributedString
提问by Martin Muldoon
Edit - This has been marked as duplicate, but as I state below, I am looking for a Swift solution. Everything I've found is written in Objective C.
编辑 - 这已被标记为重复,但正如我在下面所述,我正在寻找一个 Swift 解决方案。我发现的所有内容都是用 Objective C 编写的。
I am trying to convert HTML into an NSAttributedString, but can't figure how to set the font style and size. All of the examples are in Objective C which I'm not skilled in. How can I set the font style/size to System 15 (as an example)
我正在尝试将 HTML 转换为 NSAttributedString,但无法确定如何设置字体样式和大小。所有示例都在我不熟悉的目标 C 中。如何将字体样式/大小设置为 System 15(作为示例)
private func stringFromHtml(string: String) -> NSAttributedString? {
do {
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
if let d = data {
let str = try NSAttributedString(data: d,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
return str
}
} catch {
}
return nil
}
EDIT...... I've tried several ways. I can't get it to work. I'm now getting an error message: Type of expression is ambiguous without more context. It's pointing to the NSAttributedString I've tried the following:
编辑......我尝试了几种方法。我无法让它工作。我现在收到一条错误消息:表达式类型不明确,没有更多上下文。它指向 NSAttributedString 我尝试了以下操作:
let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
if let d = data {
let str = try NSAttributedString(data: d,
attributes: myAttribute,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
回答by Vikas Rajput
let myString = "Swift Attributed String"
let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
// set attributed text on a UILabel
myLabel.attributedText = myAttrString
font
字体
let myAttribute = [ NSFontAttributeName: UIFont(name: "Chalkduster", size: 18.0)! ]
Shadow
阴影
let myShadow = NSShadow()
myShadow.shadowBlurRadius = 3
myShadow.shadowOffset = CGSize(width: 3, height: 3)
myShadow.shadowColor = UIColor.gray
let myAttribute = [ NSShadowAttributeName: myShadow ]
Underline
强调
let myAttribute = [ NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue ]
Textcolor
文字颜色
let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]
BackGroud Color
背景颜色
let myAttribute = [ NSBackgroundColorAttributeName: UIColor.yellow ]
回答by Osman
Swift 4 You can use :
斯威夫特 4 您可以使用:
let stringWithAttribute = NSAttributedString(string: selectedFilter ?? "",
attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14.0)])
回答by Alan Steiman
The only thing that worked for me is to wrap the HTML in a <span>
and apply the style there:
唯一对我有用的是将 HTML 包装在 a 中<span>
并在那里应用样式:
let modifiedFontString = "<span style=\"font-family: Lato-Regular; font-size: 14; color: rgb(60, 60, 60)\">" + originalHTMLString + "</span>"
Try this extension:
试试这个扩展:
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return NSAttributedString() }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return NSAttributedString()
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
func convertToAttributedString() -> NSAttributedString? {
let modifiedFontString = "<span style=\"font-family: Lato-Regular; font-size: 14; color: rgb(60, 60, 60)\">" + self + "</span>"
return modifiedFontString.htmlToAttributedString
}
}
And you use it like this:
你像这样使用它:
someLabel.attributedString = someHTMLString.convertToAttributedString()
Hope it helps!
希望能帮助到你!
回答by iOS
var attrString = NSAttributedString(string: "labelText", attributes: [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont(name: "system", size: 12)])
label.attributedText = attrString
回答by Sakshi
First you need to convert data to string then use this peice of code
首先,您需要将数据转换为字符串,然后使用这段代码
let attributes = [
NSForegroundColorAttributeName: UIColor.black,
]
try NSAttributedString(string: "", attributes: attributes)
回答by iNiravKotecha
you can set this way
你可以这样设置
let attribute: [String : Any] = [NSFontAttributeName: UIFont.systemFont(ofSize: UIFont.systemFontSize, weight: UIFontWeightThin),
NSForegroundColorAttributeName: UIColor.red]
let attributedText = NSAttributedString(string: "Your String", attributes: attribute)