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

时间:2018-12-05 08:48:55

标签: swift dynamic size collectionview

我正在使用collectionView 其每个单元格都具有以下内容:enter image description here

一切正常,直到我在iPhone 5s上对其进行测试:enter image description here

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

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
}

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

1 个答案:

答案 0 :(得分:0)

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