簡體   English   中英

如何始終觀察標簽文本並在Swift 4中更改顏色?

[英]How to always observe the text of label and change color in swift 4?

我的標簽對於比特幣應用程序總是在變化,我試圖將自定義UILabel類附加到我的標簽上,以顯示過去一天的+和-價格變化。 如果增加+,我想將標簽更改為綠色,將-更改為紅色。 我怎樣才能做到這一點?

編輯:

我需要將一個自定義UIlabel類附加到情節提要上的Labels上,該類始終讀取轉換為int的字符串值,如果值<0,則將顏色更改為紅色,如果值> 0,則將顏色更改為綠色

你可以試試

var ob:NSKeyValueObservation?

//

ob = lbl.observe(\.text) { (lbl,ob) in

    // check text here and change color
}

要么

class Cus:UILabel {

 override var text:String? {
    didSet{
       // ! won't crash as long as you guarantee int value is set
        self.textColor = Int(text!)! < 0 ? UIColor.red : UIColor.green

    }
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM