我正在尝试使用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
}()
我希望获得清晰的背景,而不是灰色的背景。
答案 0 :(得分:1)
删除
NSAttributedString.Key.backgroundColor: UIColor.clear
来自属性字典。
添加
textView.backgroundColor = .clear
答案 1 :(得分:0)
您还需要设置UITextView的backgroundColor。在return语句之前编写以下代码。
textView.backgroundColor = UIColor.clear