简体   繁体   English

在不占据全屏宽度的情况下获取UILabel宽度?

[英]Obtaining UILabel width when not occupying full screen width?

I would like to obtain the width of a UILabel added as a subView inside a custom TableView Cell. 我想获得UILabel的宽度,该UILabel作为子View添加到自定义TableView Cell中。 The TableView Class I am using is listed below: 下面列出了我正在使用的TableView类:

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”)

}

}

However, from the above code when I assigned the rightAnchor constraint for the customLabel UILabel, Xcode did not return the correct width of the UILabel I was looking for. 但是,从上面的代码中,当我为customLabel UILabel分配了rightAnchor约束时,Xcode并未返回我正在寻找的UILabel的正确宽度。

I understand that I only specified the top and left constraints for the UILabel. 我了解我只为UILabel指定了顶部和左侧约束。 I also know that UILabel by default has intrinsic layout on, that it can decide on the required height based on the content of the UILabel. 我也知道UILabel默认具有固有的布局,它可以根据UILabel的内容确定所需的高度。 However, I am wondering if I did not set the numberOfLines for the customUILabel defined in the above code as 0 (ie, I only want my text inside the UILabel to occupy one line only). 但是,我想知道是否没有将上述代码中定义的customUILabel的numberOfLines设置为0(即,我只希望UILabel中的文本仅占据一行)。 Can I obtain the width of the customUILabel before the text got truncated. 我可以在截断文本之前获取customUILabel的宽度吗?

Let me explain further, if my customLabel has a lot of text, it will occupy the full width of the screen then gets truncated. 让我进一步解释一下,如果我的customLabel有很多文本,它将占据屏幕的整个宽度,然后被截断。 However, if it does not contain a lot of text, then it's width will be less than the width of the screen. 但是,如果它不包含很多文本,则它的宽度将小于屏幕的宽度。 And this is exactly what I am interested in obtaining, the width of the UILabel used to display the small text inside it? 这正是我有兴趣获得的,用于在其中显示小文本的UILabel的宽度吗?

Regards, Shadi. 问候,沙迪。

You need 你需要

self.contentView.rightAnchor.constraintGreaterThanOrEqualToAnchor(customLabel.rightAnchor, constant: 8.0).active = true

Then print the width inside 然后在里面打印宽度

override func layoutSubviews() {
   super.layoutSubviews()
   print(customLabel.frame.width)
}

Don't use frames in constraints calculations 不要在约束计算中使用框架

(contentView.frame.size.width/2)-10-customLabel.frame.size.width

Inside cell init they not yet calculated 内部单元init尚未计算

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM