向宽度可变的两个标签添加右约束
Add right constraint to two labels with variable width
我在视图中有两个彼此相邻的 UILabel,我将在其中显示对 post 的评论,一个用于用户名,一个用于时间戳。我将时间戳转换为一种简单的格式,例如一小时前的“1h”,22 分钟前的“22m”等等。
这是我的两个标签:
var usernameLabel: UILabel = {
let usernameLabel = UILabel()
usernameLabel.numberOfLines = 1
usernameLabel.lineBreakMode = .byTruncatingTail
usernameLabel.font = UIFont.boldSystemFont(ofSize: 15)
usernameLabel.textColor = UIColor.darkGray
usernameLabel.translatesAutoresizingMaskIntoConstraints = false
usernameLabel.text = "Username"
usernameLabel.isUserInteractionEnabled = true
usernameLabel.isExclusiveTouch = false
usernameLabel.backgroundColor = .green
return usernameLabel
}()
var commentDateLabel: UILabel = {
let commentDateLabel = UILabel()
commentDateLabel.font = UIFont.systemFont(ofSize: 12)
commentDateLabel.textColor = UIColor.lightGray
commentDateLabel.translatesAutoresizingMaskIntoConstraints = false
commentDateLabel.backgroundColor = .red
return commentDateLabel
}()
我为它们都添加了约束以确保它们适合我的视图,如下所示:
commentDateLabel.leftAnchor.constraint(equalTo: usernameLabel.rightAnchor, constant: 8).isActive = true
commentDateLabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -16).isActive = true
usernameLabel.leftAnchor.constraint(equalTo: profilePictureImageView.rightAnchor, constant: 16).isActive = true
我面临的问题是,日期在最右边对齐,用户名占据了整个宽度。我希望它有所不同 - 用户名标签的宽度与需要的一样宽,日期占据了剩下的整个 space。
换句话说:我希望绿色标签缩短以刚好适合文本,红色标签占据剩余的整个宽度,因此它们彼此相邻但是当用户名太长时它会截断并仍然显示整个日期标签。我该怎么做?
您需要设置
contentHuggingPriority
到usernameLabel
要高于其他的,还要设置
contentCompressionResistance
到commentDateLabel
更高
我在视图中有两个彼此相邻的 UILabel,我将在其中显示对 post 的评论,一个用于用户名,一个用于时间戳。我将时间戳转换为一种简单的格式,例如一小时前的“1h”,22 分钟前的“22m”等等。
这是我的两个标签:
var usernameLabel: UILabel = {
let usernameLabel = UILabel()
usernameLabel.numberOfLines = 1
usernameLabel.lineBreakMode = .byTruncatingTail
usernameLabel.font = UIFont.boldSystemFont(ofSize: 15)
usernameLabel.textColor = UIColor.darkGray
usernameLabel.translatesAutoresizingMaskIntoConstraints = false
usernameLabel.text = "Username"
usernameLabel.isUserInteractionEnabled = true
usernameLabel.isExclusiveTouch = false
usernameLabel.backgroundColor = .green
return usernameLabel
}()
var commentDateLabel: UILabel = {
let commentDateLabel = UILabel()
commentDateLabel.font = UIFont.systemFont(ofSize: 12)
commentDateLabel.textColor = UIColor.lightGray
commentDateLabel.translatesAutoresizingMaskIntoConstraints = false
commentDateLabel.backgroundColor = .red
return commentDateLabel
}()
我为它们都添加了约束以确保它们适合我的视图,如下所示:
commentDateLabel.leftAnchor.constraint(equalTo: usernameLabel.rightAnchor, constant: 8).isActive = true
commentDateLabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -16).isActive = true
usernameLabel.leftAnchor.constraint(equalTo: profilePictureImageView.rightAnchor, constant: 16).isActive = true
我面临的问题是,日期在最右边对齐,用户名占据了整个宽度。我希望它有所不同 - 用户名标签的宽度与需要的一样宽,日期占据了剩下的整个 space。
换句话说:我希望绿色标签缩短以刚好适合文本,红色标签占据剩余的整个宽度,因此它们彼此相邻但是当用户名太长时它会截断并仍然显示整个日期标签。我该怎么做?
您需要设置
contentHuggingPriority
到usernameLabel
要高于其他的,还要设置
contentCompressionResistance
到commentDateLabel
更高