ios - 居中对齐 UILabel 文本?

标签 ios swift

我正在尝试将 UILabel 的文本居中对齐,如图所示:

enter image description here

我的代码示例:

myLabel.textAlignment = .Justified myLabel.lineBreakMode = .ByWordWrapping

我得到的结果:

enter image description here

我错过了什么吗?

最佳答案

我在那里找到了答案:https://stackoverflow.com/a/27548566/833816

它通过添加 paragraphStyle.firstLineHeadIndent = 0.001 来工作

完整示例:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Justified
paragraphStyle.firstLineHeadIndent = 0.001

let mutableAttrStr = NSMutableAttributedString(attributedString: detailsLabel.attributedText)
mutableAttrStr.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, mutableAttrStr.length))
myLabel.attributedText = mutableAttrStr

关于ios - 居中对齐 UILabel 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29754199/

相关文章:

ios - 为 ui 组件提供绝对值 - 不适用于 iPad

ios - 在 iOS 中使用 Cordova 打开本地 pdf

swift - 如何从双重计算中删除前导零?

ios - 为 arm64 iOS 构建 C 库 (GMP)

ios - 如何检查模态视图当前是否在我的 self.window.rootViewController 之上?

ios - 使 MKAnnotation 地标始终可见

swift - WatchOS InterfaceMap 更新用户在 map 上的位置

swift - 函数和闭包有什么区别?

ios - 在运行时使用按钮更改应用程序语言

swift - 索引的不同行为(在 :) in Collection and String 之后