【问题标题】:Swift Attributed String Clear Background TextSwift 属性字符串清除背景文本
【发布时间】:2019-04-07 06:58:04
【问题描述】:

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

尝试过 UIColor.blue,但我仍然不明白怎么回事

【问题讨论】:

  • 可能这个gray背景来自UITextView

标签: ios swift xcode text fonts


【解决方案1】:

删除

NSAttributedString.Key.backgroundColor: UIColor.clear

来自属性字典。

添加

textView.backgroundColor = .clear

【讨论】:

    【解决方案2】:

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

    textView.backgroundColor = UIColor.clear
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多