代码之家  ›  专栏  ›  技术社区  ›  Joel Prine

Swift 4-设置需要显示和布局不需要在自身上重新绘制UILabel。UIView视图

  •  0
  • Joel Prine  · 技术社区  · 7 年前

    基本上,我有一个自定义UILabel子类,它根据变量设置标签颜色。

    class MyLabel: UILabel {
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            textColor = GlobalVars.mainTextColor
        }
    }
    

    在视图控制器上,我有一个按钮,用于设置白色背景,并将文本颜色变量设置为黑色:

    @IBAction func btnWhite(_ sender: UIButton) {
            GlobalVars.mainTextColor = UIColor.black
            GlobalVars.bgColor = UIColor.white
            self.view.backgroundColor = UIColor.white
            self.view.setNeedsDisplay()
            self.view.layoutIfNeeded()
            DispatchQueue.main.async { [weak self] in
                self?.view.setNeedsLayout()
                self?.view.layoutIfNeeded()
            }
        }
    

    单击此项并更新变量后,我希望ViewController重新绘制标签,这将更新其文本颜色。如果我弹出VC并返回,这很好,但我希望在单击按钮时显示更新。我在视图上有一堆标签,它们都设置为myLabel类。我不想手动对屏幕上的每个标签进行代码更改,我只想重新绘制它。UIView不是自定义类,只是视图控制器附带的默认UIView。这应该已经在主线程上发生了,但我已经尝试添加调度。主要的异步以防万一。我预计最终这两者都不需要。 Image of my view controller layout here

    当我单击按钮时,背景变为白色,但标签文本颜色不会更新,我相信这是因为它没有重新绘制它们。还有第二个按钮btnBlack,可以将其切换到与亮/暗模式效果完全相反的位置。

    思想?

    3 回复  |  直到 6 年前
        1
  •  0
  •   Bilal hao zou    7 年前

    无论何时 GlobalVars.mainTextColor GlobalVars.bgColor 值更改时,必须再次设置所有标签的颜色。

    一种选择是使用通知。您可以在上发布通知 didSet 并在 MyLabel 类并更新颜色。

    class GlobalVars {
        static let onChangeColorNotification = "onChangeColorNotification"
        static var mainTextColor:UIColor? {
            didSet {
                NotificationCenter.default.post(Notification.init(name: Notification.Name(rawValue: onChangeColorNotification)))
            }
        }
    }
    

    在你的 MyLabel公司

    class MyLabel: UILabel {
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            onChangeGlogabColor()
            NotificationCenter.default.addObserver(self, selector: #selector(onChangeGlogabColor), name: NSNotification.Name(rawValue: GlobalVars.onChangeColorNotification), object: nil)
        }
        @objc func onChangeGlogabColor() {
            textColor = GlobalVars.mainTextColor
        }
    }
    

    您可以对所有自定义类执行相同的操作,如 MyButton MyView ,捕获通知并更新颜色。

    你不必打电话 setNeedsDisplay() layoutIfNeeded() .

    @IBAction func btnWhite(_ sender: UIButton) {
        GlobalVars.mainTextColor = UIColor.black
        GlobalVars.bgColor = UIColor.white
        self.view.backgroundColor = UIColor.white        
    }
    
        2
  •  0
  •   Muhammad Waqas Bhati    7 年前

    您必须在按钮单击方法中更改TextColor

    @IBAction func btnWhite(_ sender: UIButton) {
         myCustomLabel.textColor = UIColor.blue
    } 
    

    无需调用下面提到的方法

    self.view.setNeedsDisplay()
    self.view.layoutIfNeeded()
    

    注: 没有其他方法可以更改UIlabel TextColor

        3
  •  0
  •   Joel Prine    7 年前

    比拉尔的回答是正确的。有时需要一种完全不同的方法。谢谢你的新眼光,这里是最后的代码。

    添加下面的代码,并将所有标签设置为此“MyLabel”类。然后,任何时候,全球卫星。mainTextColor变量更改后,标签将更新其文本颜色。这可以适用于UIButtons或任何需要在var更改时更新的属性。这将更新加载的任何VC上具有此类的任何标签。这也解决了我的根VC标签也需要更新的问题,因为它们也已经用原始颜色绘制,非常光滑!

    class MyLabel: UILabel {
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            textColor = GlobalVars.mainTextColor
            NotificationCenter.default.addObserver(self, selector: #selector(onChangeGlobalColor), name: NSNotification.Name(rawValue: "onChangeColorNotification"), object: nil)
        }
        @objc func onChangeGlobalColor() {
            textColor = GlobalVars.mainTextColor
        }
    }
    
    struct GlobalVars {
        static var mainTextColor:UIColor = UIColor() {
            didSet {
                NotificationCenter.default.post(Notification.init(name: Notification.Name(rawValue: "onChangeColorNotification")))
            }
        }
    
    @IBAction func btnWhite(_ sender: UIButton) {
            GlobalVars.mainTextColor = UIColor.black
            self.view.backgroundColor = UIColor.white
        }
    @IBAction func btnBlack(_ sender: UIButton) {
            GlobalVars.mainTextColor = UIColor.white
            self.view.backgroundColor = UIColor.black
        }