简体   繁体   English

UITextView 不会改变 textColor 属性

[英]UITextView does not change textColor property

I have a UITextView custom class:我有一个 UITextView 自定义类:

class TitleTextView: UITextView {

    override func layoutSubviews() {
        super.layoutSubviews()
        setup()
    }

    func setup() {
        textContainerInset = UIEdgeInsets.zero
        textContainer.lineFragmentPadding = 0
        textColor = .brand100
        backgroundColor = .clear
        isUserInteractionEnabled = false
        textAlignment = .left
        isScrollEnabled = false
        let frameWidth = Constants.screenSize.width * 87.5 / 100
        font = UIFont.OpenSans(.semibold, size: (frameWidth * 8.55 / 100))
    }
 }

I used this text view custom class inner a UIView.我在 UIView 内部使用了这个文本视图自定义类。

class MyCustomHeaderView: UIView{

    @IBOutlet weak var titleTextView: TitleTextView!

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func layoutSubviews() {

        backgroundColor = .brand100

        titleTextView.text = "Market Place"
        titleTextView.textColor = .brand400

        layoutIfNeeded()

    }    

}

And I called this UIView in a UIViewController.我在 UIViewController 中调用了这个 UIView。

private func setupTitleView() {
        let titleView = UINib(nibName: "TitleView", bundle: .main).instantiate(withOwner: nil, options: nil).first as! UIView
        titleView.frame = contentHeaderView.bounds
        contentHeaderView.addSubview(titleView)        
        view.layoutIfNeeded()
}

But when I set the textColor property in my custom UIView (MyCustomHeaderView) the color doesn't change.但是当我在我的自定义 UIView (MyCustomHeaderView) 中设置 textColor 属性时,颜色不会改变。

Do you have any idea about why the reason that my UITextView doesn't apply the color that I set in my custom UIView?您知道为什么我的 UITextView 不应用我在自定义 UIView 中设置的颜色吗? I called layoutIfNeed() but this doesn't work.我调用了layoutIfNeed()但这不起作用。

It's because you are doing everything inside the layoutSubviews Which in itself is really bad practice.这是因为您在layoutSubviews中执行所有操作,这本身就是非常糟糕的做法。

In your case you instantiate the CustomHeaderView and the layout for that is called, hence calling layoutSubviews next step is that the textView is added to your CustomHeaderView and then the textView's layoutSubviews is called and will override your color.在你的情况,你实例化CustomHeaderView和该布局被调用,因此调用layoutSubviews下一步是将textView被添加到您的CustomHeaderView然后TextView的的layoutSubviews被调用,将覆盖你的颜色。

You can solve this in two ways i believe.我相信你可以通过两种方式解决这个问题。 Altho i don't work with Nibs and storyboards,尽管我不使用笔尖和故事板,

first:第一的:

class MyCustomHeaderView: UIView{

    @IBOutlet weak var titleTextView: TitleTextView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    func setup() {

        backgroundColor = .brand100

        titleTextView.text = "Market Place"
        titleTextView.textColor = .brand400

        layoutIfNeeded()

    }    

}

Second, this is a big maybe:其次,这是一个很大的可能:

class MyCustomHeaderView: UIView{

    @IBOutlet weak var titleTextView: TitleTextView!

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func layoutSubviews() {
        defer {
            backgroundColor = .brand100

            titleTextView.text = "Market Place"
            titleTextView.textColor = .brand400

            layoutIfNeeded()
        }
    }    

}

Defer will wait till everything is been initialised before running whatever is in the block.在运行块中的任何内容之前,Defer 将等到一切都被初始化。 I don't know tho how that works with layoutSubviews我不知道它如何与 layoutSubviews 一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM