Swift属性字符串清晰的背景文本

时间:2019-04-07 06:58:05

标签: ios swift xcode text fonts

我正在尝试使用NSMutableAttributedString创建文本标题,但是在属性字典中,我相信我有正确的代码,但是当我运行代码时,总是有灰色背景,那我在做什么错呢?还是我可能会错过什么?

let titleTextView: UITextView = {
    let textView = UITextView()

    let attributedText = NSMutableAttributedString(string: "Wake Up Happy", attributes: [NSAttributedString.Key.font: UIFont.init(name: "Marker Felt", size: 40)!, NSAttributedString.Key.backgroundColor: UIColor.clear])

    textView.attributedText = attributedText

    textView.textAlignment = .center
    textView.isEditable = false
    textView.isScrollEnabled = false
    textView.translatesAutoresizingMaskIntoConstraints = false
    return textView
}()

我希望获得清晰的背景,而不是灰色的背景。

具有UIColor.clear的图像
Link to image with UIColor.clear

试图做UIColor.blue,但我仍然没明白是怎么回事
Tried doing UIColor.blue and still I don't get what's wrong

2 个答案:

答案 0 :(得分:1)

删除

NSAttributedString.Key.backgroundColor: UIColor.clear

来自属性字典。

添加

textView.backgroundColor = .clear

答案 1 :(得分:0)

您还需要设置UITextView的backgroundColor。在return语句之前编写以下代码。

textView.backgroundColor = UIColor.clear