在带有NSAttributedString的UILabel中居中文本 [英] centering text in a UILabel with an NSAttributedString

查看:662
本文介绍了在带有NSAttributedString的UILabel中居中文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我正在处理的应用程序进行一些基本改进.在iOS快速开发领域还是一个新手.我发现代码中的文本行将自动居中,因为我将标签设置为居中.经过一番研究,我发现情况并非如此.我如何将这样的代码对齐到中心:

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:

let atrString = try NSAttributedString(
   data: assetDetails!.cardDescription.dataUsingEncoding(NSUTF8StringEncoding)!, 
   options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], 
   documentAttributes: nil)
assetDescription.attributedText = atrString

推荐答案

您需要创建一个指定中心对齐方式的段落样式,并将该段落样式设置为文本的属性.游乐场示例:

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

结果:

由于要解析HTML文档以创建属性字符串,因此需要在创建后添加属性,如下所示:

Since you're parsing an HTML document to create your attributed string, you'll need to add the attribute after creation, like this:

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

Swift 4更新

在Swift 4中,属性名称现在为NSAttributeStringKey类型,而标准属性名称是该类型的静态成员.因此,您可以像这样添加属性:

Update for 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:

richText.addAttribute(.paragraphStyle, value: style, range: NSMakeRange(0, richText.length))

这篇关于在带有NSAttributedString的UILabel中居中文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆