ios - 将长文本放入 UILabel 中的多行
问题描述
在我的 UI 中有一个UILabel
,并且在运行时将文本设置为它。如果文本太长,它应该显示为两行。
UILabel 的代码如下。
lazy var disLabel:UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textAlignment = .left
label.font = Fonts.fontForType(fontType: .AktivGroteskRegular, fontSize: 15.0)
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
label.backgroundColor = UIColor.red
label.text = "Hello world. How are you. XaXaXa
return label
}()
然后我设置约束如下。
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
label.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
输出如下。
这里有足够的空间来写“你”这个词。但它已经走到了第二行。无论文本是什么,第一行总是有很多空间。
如何解决这个问题。
解决方案
推荐阅读
- spring - 微服务中的 Zuul 路由
- postgresql - 是否有与 Jenkins 管道兼容的 PL/pgSQL 或 PostgreSQL 代码质量检查的代码分析器?
- angular - 为什么 MatDialog 没有显示?
- elasticsearch - 在elasticsearch中按嵌套字段的大小获取文档?
- cmake - CMake 和 Ninja - “缺少并且没有已知的规则来制作它”
- javascript - Moment.js 以 ISO 格式向当前日期添加 7 天
- c# - 如何将我的数据库设置回初始值
- c++ - volatile关键字c ++的行为
- html - 如何在 CSS 中为所有子类正确设置样式?
- jboss - Keycloak LDAP 用户身份验证不起作用