【问题标题】:Change the colour of the text UITextField to the colour typed in it将文本 UITextField 的颜色更改为其中键入的颜色
【发布时间】:2017-07-28 18:41:40
【问题描述】:

我有一个UITextView,我需要检测String,这将是用户输入的颜色。然后我需要将UITextField中的文字颜色改为用户输入的颜色。

例如:如果我在textField 中输入“红色”,则文本的颜色应变为红色。

有谁知道如何做到这一点?

谢谢!

【问题讨论】:

  • 到目前为止你有没有尝试过?你的代码是什么样的?
  • @ZGski 我只为文本字段创建了一个基本布局并连接到它。对如何处理它感到困惑。
  • 老实说,我认为如果您自己试一试并自行测试,这将对每个人都有好处。 Stack Overflow 更多地用于调试或当您完全被难住时 - 而不是请求诸如“我应该如何制作这个东西”之类的东西。

标签: swift uitextfield uicolor


【解决方案1】:

首先,您需要创建StringUIColor 的映射。

let colorMapping: [String: UIColor] = [
    "green": .green,
    "white": .white,
    //etc...
]

【讨论】:

    【解决方案2】:

    首先将每种颜色的文本映射到相应的UIColor

    let colors: [String: UIColor] = [
        // add any built-in colors
        "red": .red,
        "blue": .blue,
        // custom colors too!
        "goldenrod": UIColor(red: 218, green: 165, blue: 32) 
        // add all the colors you wish to detect
        // ...
        // ...
    ]
    

    使包含您的文本字段的视图控制器符合UITextViewDelegate 并实现shouldChangeTextIn 函数(在用户输入/删除字符时调用):

    class MyViewController: UIViewController, UITextFieldDelegate {
    
        // ... other view controller code
    
        func textField(_ textField: UITextField, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    
            if let currentText = textField.text {
                // check if the text is in the `colors` dictionary
                //   and if so, apply the color to the `textColor`
                if colors.keys.contains(currentText) {
                    textField.textColor = colors[currentText]
                }
            }
    
            return true
    
        }
    
        // ... other view controller code
    
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用以下委托方法检测文本字段中的文本

      func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
      {
              //Don't forgot to make user entered content to lowercaseString while checking with colorCollection
      
              if colorCollection.keys.contains(textField.text.lowercaseString) {
                  textField.textColor = colorCollection[currentText] //If entered content match with any colour then it will change the text colour of textfield  
              }
      
          return true
      }
      

      创建简单的颜色集合,如下所示

      let colorCollection: [String: UIColor] = [
          "blue": .blue,
          "purple": .purple,
          //your other coolers 
      ] 
      

      【讨论】:

        【解决方案4】:

        所以假设你有 textfield 和 textview 出口或以编程方式创建:-

        let textField = UITextField()
        let txtVw = UITextView()
         //If enter some text inside textfield for instance red then get the value, compare it and set the text color on textview
        if textField.text?.lowercased() == "red" {
         txtVw.textColor = UIColor.red
        } 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-02-25
          • 2020-09-08
          • 1970-01-01
          • 1970-01-01
          • 2010-11-23
          • 1970-01-01
          • 2011-04-28
          • 1970-01-01
          相关资源
          最近更新 更多