简体   繁体   English

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

[英]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.

相关问题 如何根据文本字段的大小调整字体大小? - How can I adjust the font size according to the size of the text field? 更改归因文本字体大小并保持格式 - Change Attributed Text Font Size and Keep Formatting 如何在UIButton中调整文本的字体大小以在Swift中以编程方式适应宽度? - How to adjust font size of text in a UIButton to fit the width programmatically in Swift? 使用 Swift 在带有属性文本的 UITextView 中的光标点设置特定字体大小 - Setting particular font-size at the cursor point in UITextView with attributed text using Swift Swift - 使用 Storyboard 的属性文本功能在以编程方式设置字体大小并运行模拟器时全部重置 - Swift - Attributed Text features using Storyboard all reset when setting font size programmatically and run simulator 如何将属性字符串(文本)保存到文件中(swift,cocoa)? - How can I save the attributed string (text) into file (swift, cocoa)? 如何调整UITabBarController的标题文本字体大小? - How do I adjust the title text font size for my UITabBarController? 属性文本,使用swift将一种特定的字体替换为另一种 - Attributed text, replace a specific font by another using swift 如何更改文本视图中的字体大小,而不更改字体样式? (轻量还是常规)-迅捷 - How can I change the font size in a text view, but not the font style? (light vs. regular) - Swift 如何快速设置动态字体? - How can i set dynamic font in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM