首页 > 解决方案 > 如何在 UItextField 中添加不同字体的字符串?

问题描述

我正在尝试添加具有不同字体参数的两部分文本。但他们总是从第一个字符串中获得价值。还有一件奇怪的事情,字体颜色是独立设置的。

let descriptionTextView: UITextView = {
    let view = UITextView()


    let attributedText = NSMutableAttributedString(string: "Text", attributes:
        [.font:UIFont.boldSystemFont(ofSize: 20),
    .foregroundColor: UIColor.blue])

    attributedText.append(NSAttributedString(string: "\n\n\n Other text", attributes:
        [.font: UIFont.italicSystemFont(ofSize: 5),
         .foregroundColor: UIColor.gray]))

    view.attributedText = attributedText


    view.translatesAutoresizingMaskIntoConstraints = false
    view.textAlignment = .center
    view.font = UIFont.boldSystemFont(ofSize: 20)
    view.isEditable = false
    view.isScrollEnabled = false

    return view
}()

这是它在模拟器中的样子

标签: swiftuitextviewnsattributedstring

解决方案


消除

view.font = UIFont.boldSystemFont(ofSize: 20)

推荐阅读