Swift归因字符串清除背景文本

问题描述 投票:0回答:2

我正在尝试使用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

ios swift xcode text fonts
2个回答
1
投票

去掉

NSAttributedString.Key.backgroundColor: UIColor.clear

来自属性字典。

textView.backgroundColor = .clear

0
投票

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

textView.backgroundColor = UIColor.clear
© www.soinside.com 2019 - 2024. All rights reserved.