Ios 将UILabel中的文本与NSAttribute字符串居中
正在对我正在开发的应用程序进行一些基本的改进。对于iOS swift开发场景来说仍然是新的。我认为代码中的文本行将自动居中,因为我将标签设置为居中。经过一点研究,我发现事实并非如此。我如何将这样的代码对齐到中心:Ios 将UILabel中的文本与NSAttribute字符串居中,ios,swift,uilabel,nsattributedstring,Ios,Swift,Uilabel,Nsattributedstring,正在对我正在开发的应用程序进行一些基本的改进。对于iOS swift开发场景来说仍然是新的。我认为代码中的文本行将自动居中,因为我将标签设置为居中。经过一点研究,我发现事实并非如此。我如何将这样的代码对齐到中心: let atrString = try NSAttributedString( data: assetDetails!.cardDescription.dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocume
let atrString = try NSAttributedString(
data: assetDetails!.cardDescription.dataUsingEncoding(NSUTF8StringEncoding)!,
options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],
documentAttributes: nil)
assetDescription.attributedText = atrString
您需要创建指定中心对齐的段落样式,并将该段落样式设置为文本的属性。操场示例:
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文档以创建属性字符串,因此需要在创建后添加属性,如下所示:
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
richText.addAttribute(.paragraphStyle, value: style, range: NSMakeRange(0, richText.length))
Swift 4的更新
在Swift 4中,属性名称现在是类型NSAttributeStringKey
,标准属性名称是该类型的静态成员。因此,您可以添加如下属性:
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
richText.addAttribute(.paragraphStyle, value: style, range: NSMakeRange(0, richText.length))
在Swift 4.1中:
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.center
lbl.centerAttributedText = NSAttributedString(string: "Total Balance",attributes: [.paragraphStyle: style])
(为代码块编辑)您可以使用此实用程序功能对标签进行所有常见配置
@discardableResult
public func DULabel(text: String, frame: CGRect = .zero, parent:UIView? = nil , font:UIFont, textColor:UIColor = .black, numOfLines:Int = 0 ,textAlignment: NSTextAlignment = .center,lineSpaceing:CGFloat = 0, cb: ((UILabel)->Void)? = nil )-> UILabel! {
let label = UILabel()
label.frame = frame
label.font = font
label.textColor = textColor
label.textAlignment = textAlignment
label.numberOfLines = numOfLines
if( lineSpaceing == 0 ){
label.text = text
}
else {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpaceing
paragraphStyle.alignment = textAlignment
let attrString = NSMutableAttributedString(string: text)
attrString.addAttribute(.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
label.attributedText = attrString
}
if let parent = parent {
parent.addSubview(label)
}
cb?(label)
return label
}
因此,在您的代码示例中:let richText=nsmutableAttributeString(string:“正在对我正在处理的应用程序进行一些基本改进。对于iOS swift开发场景来说仍然是新的。我认为代码中的文本行将自动居中,因为我将标签设置为居中。”,属性:[NSParagraphStyleAttributeName:style])我想用我的示例中的atrString替换“”字符串?工作非常好,非常感谢!我现在也理解了它,这是一个巨大的优点!在Swift 4
中[kCTParagraphStyleAttributeName作为NSAttributedStringKey:style]
在Swift 4中,您可以说.paragraphStyle
作为属性名称。什么是CenterAttributeText?