不占全屏宽度时获取UILabel宽度?
Obtaining UILabel width when not occupying full screen width?
我想获取在自定义 TableView 单元格中作为子视图添加的 UILabel 的宽度。我正在使用的 TableView Class 如下所示:
import UIKit
class customTableViewCell: UITableViewCell {
let customLabel: UILabel = {
let label = UILabel()
label.translateAutoConstraints = false
label.textAlignment = .left
return label
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(customLabel)
customLabel.topAnchor.constraint(equal: contentView.topAnchor).isActive = true
customLabel.leftAnchor.constraint(equal: contentView.leftAnchor, constant: 10).isActive = true
customLabel.rightAnchor.constraint(equal: contentView.rightAnchor, constant: (contentView.frame.size.width/2)-10-customLabel.frame.size.width).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError(“init(coder:) has not been implemented”)
}
}
但是,根据上面的代码,当我为 customLabel UILabel 分配 rightAnchor 约束时,Xcode 没有 return 我正在寻找的 UILabel 的正确宽度。
我知道我只为 UILabel 指定了顶部和左侧约束。我还知道 UILabel 默认情况下具有内部布局,它可以根据 UILabel 的内容决定所需的高度。但是,我想知道我是否没有将上面代码中定义的 customUILabel 的 numberOfLines 设置为 0(即,我只希望 UILabel 中的文本只占一行)。我可以在文本被截断之前获取 customUILabel 的宽度吗?
让我进一步解释一下,如果我的customLabel 有很多文本,它会占据屏幕的整个宽度然后被截断。但是,如果它不包含很多文本,那么它的宽度将小于屏幕的宽度。而这正是我感兴趣的,用于显示其中小文本的 UILabel 的宽度?
此致,
沙迪.
你需要
self.contentView.rightAnchor.constraintGreaterThanOrEqualToAnchor(customLabel.rightAnchor, constant: 8.0).active = true
然后打印里面的宽度
override func layoutSubviews() {
super.layoutSubviews()
print(customLabel.frame.width)
}
不要在约束计算中使用框架
(contentView.frame.size.width/2)-10-customLabel.frame.size.width
单元格内部 init
他们尚未计算
我想获取在自定义 TableView 单元格中作为子视图添加的 UILabel 的宽度。我正在使用的 TableView Class 如下所示:
import UIKit
class customTableViewCell: UITableViewCell {
let customLabel: UILabel = {
let label = UILabel()
label.translateAutoConstraints = false
label.textAlignment = .left
return label
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(customLabel)
customLabel.topAnchor.constraint(equal: contentView.topAnchor).isActive = true
customLabel.leftAnchor.constraint(equal: contentView.leftAnchor, constant: 10).isActive = true
customLabel.rightAnchor.constraint(equal: contentView.rightAnchor, constant: (contentView.frame.size.width/2)-10-customLabel.frame.size.width).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError(“init(coder:) has not been implemented”)
}
}
但是,根据上面的代码,当我为 customLabel UILabel 分配 rightAnchor 约束时,Xcode 没有 return 我正在寻找的 UILabel 的正确宽度。
我知道我只为 UILabel 指定了顶部和左侧约束。我还知道 UILabel 默认情况下具有内部布局,它可以根据 UILabel 的内容决定所需的高度。但是,我想知道我是否没有将上面代码中定义的 customUILabel 的 numberOfLines 设置为 0(即,我只希望 UILabel 中的文本只占一行)。我可以在文本被截断之前获取 customUILabel 的宽度吗?
让我进一步解释一下,如果我的customLabel 有很多文本,它会占据屏幕的整个宽度然后被截断。但是,如果它不包含很多文本,那么它的宽度将小于屏幕的宽度。而这正是我感兴趣的,用于显示其中小文本的 UILabel 的宽度?
此致, 沙迪.
你需要
self.contentView.rightAnchor.constraintGreaterThanOrEqualToAnchor(customLabel.rightAnchor, constant: 8.0).active = true
然后打印里面的宽度
override func layoutSubviews() {
super.layoutSubviews()
print(customLabel.frame.width)
}
不要在约束计算中使用框架
(contentView.frame.size.width/2)-10-customLabel.frame.size.width
单元格内部 init
他们尚未计算