【问题标题】:How to change the text attribute of a UILabel in Swift?如何在 Swift 中更改 UILabel 的文本属性?
【发布时间】:2019-12-17 20:18:00
【问题描述】:

我以编程方式设置了一个 UILabel,我试图通过稍后在 ViewController 中调用的函数更改文本属性,但是当调用该函数时,questionLabel.text 保持默认值“欢迎”。

基本上我想要完成的是:

  func changeLabelText() {
            questionLabel.text = "New label text"
            print(questionLabel.text!)
        }
        changeLabelText()

        // prints "New label text"

但我实际上得到的是:

   func changeLabelText() {
            questionLabel.text = "New label text"
            print(questionLabel.text!)
        }
        changeLabelText()

        // prints "Welcome"

我的标签是这样设置的:

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

    @IBOutlet var cameraView: UIView!

        var questionLabel: UILabel {

            let label = UILabel()
            label.lineBreakMode = .byWordWrapping
            label.backgroundColor = .white
            label.textColor = .black
            label.text = "Welcome"
            label.textAlignment = .center
            label.frame = CGRect(x: 65, y: 100, width: 300, height: 65)

            return label

        } 

有什么建议吗?非常感谢!

【问题讨论】:

    标签: ios swift xcode uilabel viewcontroller


    【解决方案1】:

    当前

    var questionLabel: UILabel { 
            let label = UILabel()
            label.lineBreakMode = .byWordWrapping
            label.backgroundColor = .white
            label.textColor = .black
            label.text = "Welcome"
            label.textAlignment = .center
            label.frame = CGRect(x: 65, y: 100, width: 300, height: 65) 
            return label 
    } 
    

    是一个计算属性,因此每次访问都会获得一个新的单独实例

    questionLabel.text = "New label text" // instance 1
    print(questionLabel.text!)   // instance 2
    

    你需要一个闭包

    var questionLabel: UILabel = { 
            let label = UILabel()
            label.lineBreakMode = .byWordWrapping
            label.backgroundColor = .white
            label.textColor = .black
            label.text = "Welcome"
            label.textAlignment = .center
            label.frame = CGRect(x: 65, y: 100, width: 300, height: 65) 
            return label 
    }()
    

    【讨论】:

      【解决方案2】:

      克拉蒙, 你可以试试这个。

      假设您想更改标签的某些文本,您总是为此创建两个标签,但更改标签文本颜色的方法是错误的。您可以使用 NSMutableAttributedString 更改标签的某些文本颜色。首先,您必须找到要更改该文本颜色的文本范围,然后将文本范围设置为 NSMutableAttributedString 对象为与完整字符串相比,然后使用 NSMutableAttributedString 对象设置标签属性文本。示例:

      let strNumber: NSString = "Hello Test" as NSString // you must set your  
      let range = (strNumber).range(of: "Test")
      let attribute = NSMutableAttributedString.init(string: strNumber)
      attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: range)
      yourLabel.attributedText = attribute
      

      如果你想在你的应用程序中多次使用它,你可以创建 UILabel 的扩展,它会变得更简单:-

      extension UILabel {
      func halfTextColorChange (fullText : String , changeText : String ) {
          let strNumber: NSString = fullText as NSString
          let range = (strNumber).range(of: changeText)
          let attribute = NSMutableAttributedString.init(string: fullText)
          attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: range)
          self.attributedText = attribute
      }
      

      }

      使用您的标签:-
      yourLabel = "你好测试" yourLabel.halfTextColorChange(fullText: totalLabel.text!, changeText: "Test")

      【讨论】:

        【解决方案3】:

        将计算变量更改为惰性初始化器,如下所示:

        
        lazy var questionLabel: UILabel = {
        
            let label = UILabel()
            label.lineBreakMode = .byWordWrapping
            label.backgroundColor = .white
            label.textColor = .black
            label.text = "Welcome"
            label.textAlignment = .center
            label.frame = CGRect(x: 65, y: 100, width: 300, height: 65)
        
            return label
        
        }()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多