【发布时间】:2019-07-06 21:22:59
【问题描述】:
如何根据给定的评论文本,根据需要多次制作 UIlabel Wrap?
我尝试了很多方法,例如:
self.commentText.contentMode = .scaleToFill
self.commentText.numberOfLines = 0
// self.commentText.leadingMargin(pixel: 10)
// self.commentText.trailingMargin(pixel: 10)
我从 [Qs like.] 和UILabel - auto-size label to fit text?1 得到的
但我无法使包装工作
我如何设置:
private func setupTheCommentText() {
self.commentText.frame = CGRect(x: 85, y: 35, width: 272, height: 15)
self.commentText.textColor = UIColor(red: 92/255, green: 92/255, blue: 92/255, alpha: 1)
self.commentText.font = UIFont(name: "SFCompactDisplay-Medium", size: 13)
//self.commentText.numberOfLines = 0
//self.commentText.contentMode = .scaleAspectFill
//self.commentText.translatesAutoresizingMaskIntoConstraints = false
// self.commentText.leadingMargin(pixel: 10)
// self.commentText.trailingMargin(pixel: 10)
self.addSubview(commentText)
self.commentText.topAnchor.constraint(equalTo:username.bottomAnchor).isActive = true
commentText.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.commentText.topAnchor.constraint(equalTo:username.bottomAnchor),
self.commentText.leadingAnchor.constraint(equalTo:self.contentView.leadingAnchor,constant:85.0),
self.commentText.trailingAnchor.constraint(equalTo:self.contentView.trailingAnchor,constant:-15.0),//,constant:-85.0
self.commentText.bottomAnchor.constraint(equalTo:commentText.bottomAnchor)
])
}
【问题讨论】:
-
你给标签固定宽度了吗?显示您用于将标签添加到视图的代码。或者显示故事板和约束的屏幕截图。
-
从您的编辑中,您似乎混淆了使用框架和使用自动布局约束。您应该使用其中一种。您需要将其余约束添加到视图中。