ios 使用 NSAttributedString 将 UILabel 中的文本居中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33443017/
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
centering text in a UILabel with an NSAttributedString
提问by cruelty
Going through some basic improvements to a application I am working on. Still new to the iOS swift development scene. I figured that the lines of text in my code would automatically be centered because I set the label to center. After a little bit of research I discovered this is not the case. How would I align code like this to center:
对我正在开发的应用程序进行一些基本改进。仍然是 iOS swift 开发场景的新手。我认为代码中的文本行会自动居中,因为我将标签设置为居中。经过一些研究,我发现情况并非如此。我将如何将这样的代码对齐到中心:
let atrString = try NSAttributedString(
data: assetDetails!.cardDescription.dataUsingEncoding(NSUTF8StringEncoding)!,
options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],
documentAttributes: nil)
assetDescription.attributedText = atrString
回答by rob mayoff
You need to create a paragraph style specifying center alignment, and set that paragraph style as an attribute on your text. Example playground:
您需要创建一个指定居中对齐的段落样式,并将该段落样式设置为文本的一个属性。示例游乐场:
import UIKit
import PlaygroundSupport
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.center
let richText = NSMutableAttributedString(string: "Going through some basic improvements to a application I am working on. Still new to the iOS swift development scene. I figured that the lines of text in my code would automatically be centered because I set the label to center.",
attributes: [ NSParagraphStyleAttributeName: style ])
// In Swift 4, use `.paragraphStyle` instead of `NSParagraphStyleAttributeName`.
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 400))
label.backgroundColor = UIColor.white
label.attributedText = richText
label.numberOfLines = 0
PlaygroundPage.current.liveView = label
Result:
结果:
Since you're parsing an HTML document to create your attributed string, you'll need to add the attribute after creation, like this:
由于您正在解析 HTML 文档以创建属性字符串,因此您需要在创建后添加属性,如下所示:
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.center
let richText = try NSMutableAttributedString(
data: assetDetails!.cardDescription.data(using: String.Encoding.utf8)!,
options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],
documentAttributes: nil)
richText.addAttributes([ NSParagraphStyleAttributeName: style ],
range: NSMakeRange(0, richText.length))
// In Swift 4, use `.paragraphStyle` instead of `NSParagraphStyleAttributeName`.
assetDescription.attributedText = richText
Update for Swift 4
Swift 4 更新
In Swift 4, attribute names are now of type NSAttributeStringKey
and the standard attribute names are static members of that type. So you can add the attribute like this:
在 Swift 4 中,属性名称现在属于类型NSAttributeStringKey
,标准属性名称是该类型的静态成员。所以你可以像这样添加属性:
richText.addAttribute(.paragraphStyle, value: style, range: NSMakeRange(0, richText.length))
回答by sazzadhusen iproliya
In Swift 4.1 :
在 Swift 4.1 中:
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.center
lbl.centerAttributedText = NSAttributedString(string: "Total Balance",attributes: [.paragraphStyle: style])
(edited for code block)
(为代码块编辑)