繁体   English   中英

Swift:我可以使用动态字体大小来调整属性文本吗?

[英]Swift: Can I adjust attributed text with dynamic font size?

我正在使用collectionView,其每个单元格都具有以下内容: 在此处输入图片说明

一切正常,直到我在iPhone 5s上对其进行测试: 在此处输入图片说明

“胡志明市”出界。 这是我的代码:

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
}

我认为此问题是由于字体大小引起的,有人可以解决此问题吗?

谢谢大家,我解决了。 “ label.numberOfLines = 0”有效。 问题出在我的约束之下。

暂无
暂无

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

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