当文本包含线路断开和标签的台词和LINEBREACKMODE时,Uilabel在最后一行中显示不正确的文本。ByTruncatingTail
如果在下面运行代码段,它将在最后一行中显示不正确的文本,该文本具有...省略号,如该图像所示:
一旦将linebreakmode更改为bywordwrapprapped < /code>,或将
numberOflines
设置为0,然后正确显示文本。另外,如果您从文本中删除demake /n
,则文本将正确显示。
因此,我的问题是:如何在Uilabel的文本中使用A /N线路断开,并使用有限的数字线和LineBreakMode .BytruncatingTail而又不会在最后一行显示不正确的文本?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let text = "A line break challenge \nInvestigating a bug where the text in the last line is incorrect when numberOfLines is limited to for example 3. Very strange indeed. Peculiar. What to do about this? What am I missing?"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byTruncatingTail //.byWordWrapping works, but not what I want.
let attributedString = NSAttributedString(string: text, attributes: [
.paragraphStyle: paragraphStyle
])
let label = UILabel()
label.attributedText = attributedString
label.numberOfLines = 3 //0 works, but is not what I want.
label.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(label)
label.topAnchor.constraint(equalTo: view.topAnchor, constant: 40.0).isActive = true
label.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20.0).isActive = true
label.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20.0).isActive = true
}
}
If you run the code snippet below, it will show incorrect text in the last line that has the ... ellipsis, as show in this image:
Once you change the lineBreakMode to byWordWrapping
, or set the numberOfLines
to 0, then the text is displayed correctly. Also, if you remove the line break /n
from the text, then the text is displayed correctly.
So my question is: How can I use a /n line break in the text in a UILabel with a limited numberOfLines and lineBreakMode .byTruncatingTail without showing incorrect text on the last line?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let text = "A line break challenge \nInvestigating a bug where the text in the last line is incorrect when numberOfLines is limited to for example 3. Very strange indeed. Peculiar. What to do about this? What am I missing?"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byTruncatingTail //.byWordWrapping works, but not what I want.
let attributedString = NSAttributedString(string: text, attributes: [
.paragraphStyle: paragraphStyle
])
let label = UILabel()
label.attributedText = attributedString
label.numberOfLines = 3 //0 works, but is not what I want.
label.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(label)
label.topAnchor.constraint(equalTo: view.topAnchor, constant: 40.0).isActive = true
label.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20.0).isActive = true
label.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20.0).isActive = true
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论