[英]Swift: Can I adjust attributed text with dynamic font size?
I'm working with the collectionView whose each cell has the content like this: 我正在使用collectionView,其每个单元格都具有以下内容:
everything works pretty well until I test it on an iphone 5s: 一切正常,直到我在iPhone 5s上对其进行测试:
"Ho Chi Minh City" was out of bounds. “胡志明市”出界。 Here is my code:
这是我的代码:
func configureNameLabel() {
nameLabel.numberOfLines = 2
let attributedText = NSMutableAttributedString(string: post?.name ?? "", attributes: [.font: UIFont.boldSystemFont(ofSize: 15)])
attributedText.append(NSAttributedString(string: "\nSaturday, December 1, 2018 ⦁ Ho Chi Minh City ⦁ ", attributes:
[.foregroundColor: UIColor.lightGray, .font: UIFont.preferredFont(forTextStyle: .caption1)]))
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5
attributedText.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedText.string.count))
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "user-male")
attachment.bounds = CGRect(x: 0, y: -2, width: 12, height: 12)
attributedText.append(NSAttributedString(attachment: attachment))
nameLabel.attributedText = attributedText
}
I think this problem is due to the font size, can anybody fix this ? 我认为此问题是由于字体大小引起的,有人可以解决此问题吗?
Thanks guys, I solved it. 谢谢大家,我解决了。 "label.numberOfLines = 0" worked.
“ label.numberOfLines = 0”有效。 the problem is at my constraints.
问题出在我的约束之下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.