ios - UILabel 文本对齐不起作用(Swift 4)

标签 ios swift xcode uilabel anchor

我正在尝试对齐 UILabel 中的文本,但它不起作用。打印文本时标签的第二行紧贴标签的左侧。如何使两条线在标签中心对齐?谢谢。

let bioTextView: UILabel = {
    let tv = UILabel()
    tv.text = "This is sample text for a bio label for a user on this platform."
    tv.backgroundColor = .clear
    tv.textColor = .white
    tv.lineBreakMode = .byWordWrapping
    tv.numberOfLines = 0
    tv.textAlignment = NSTextAlignment.right
    return tv
}()

请找到以下问题的屏幕截图。我的要求是使文本适合拖行。

enter image description here

最佳答案

将 View 添加为 subview 后尝试设置对齐方式。

view.addSubview(self.bioTextView)
bioTextView.textAlignment = .center

关于ios - UILabel 文本对齐不起作用(Swift 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50941979/

相关文章:

ios - 我的自定义 UITableViewCell 中的所有标签都被压扁了

ios - 在iOS上启动codenameone应用程序时发出警告(“应用程序会降低设备速度”)

swift - Metal/OpenGL : How to set vertex buffer only once?

ios - 如何禁用某个 oslog 日志级别?

ios - IBDesignable 无法工作 Xcode 7 Swift 2(核心显卡)

ios - 将图标加载到 iOS 中的 UIImageView 策略

ios - 带有渐变的 CAShapeLayer

ios - 如何以编程方式滚动我的整个 View ,包括图像

ios - 使用 OpenGL ES 2 着色器重新实现的 GLImageProcessing 的性能问题

linux - 如何将 XCode 项目导入到 linux 中?