gpt4 book ai didi

ios - 考虑到我设置的约束,为什么 UILabel 的文本没有被截断?

转载 作者:行者123 更新时间:2023-12-05 01:32:47 24 4
gpt4 key购买 nike

非常简单的设置:一个 UITableViewCell 托管一个 UIImageView 和一个 UILabel

UIImageView 有一个指向单元格的前导 anchor 和固定宽度。UILabel 有一个指向 UIImageView 的前导 anchor ,一个指向单元格的尾随 anchor ,并且垂直居中。 numberOfLines 设置为 1lineBreakMode 设置为 .byTruncatingTail 并且 Autoshrink固定字体大小

根据我的理解,这是使标签截断文本的所有必要条件。然而,事实并非如此。为什么?

约束:
Constraints

属性:
Properties

结果:
Result

最佳答案

使用这个类。根据需要编辑值。
我强烈建议您以编程方式创建单元格。约束是一件棘手的事情,您将很难设置它们。在这里,我定义了对象的大小及其在单元格内的位置。在我看来,这种方式更简单、更清洁。

class tabViewCell: UITableViewCell {

var label: UILabel!
var myImageView: UIImageView!

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}

func commonInit() {
//Space between items
let sideSpace: CGFloat = 15.0
//Hight of image and label and width of image
let imageDimension: CGFloat = 25.0
//width of image + space between beginning of your cell and image + space between image and label
let labelX: CGFloat = imageDimension+(sideSpace*2)
//Dimension of the screen - space occupied by first side space, image and space between image and label - space between label and the end of your cell
//If you want to determine the dimension of the cell use contentView instead of UIScreen.main
let labelWidth: CGFloat = UIScreen.main.bounds.width-labelX-sideSpace


myImageView = UIImageView(frame: CGRect(x: sideSpace, y: 0.0, width: imageDimension, height: imageDimension))
label = UILabel(frame: CGRect(x: labelX, y: 0.0, width: labelWidth, height: imageDimension))

label.contentMode = .scaleAspectFit
label.clipsToBounds = true
label.numberOfLines = 1
label.lineBreakMode = .byTruncatingTail
label.font = UIFont.systemFont(ofSize: 15) //Change it as you please

addSubview(myImageView)
addSubview(label)
}
}

关于ios - 考虑到我设置的约束,为什么 UILabel 的文本没有被截断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65382264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com