如何在 Swift 中使用 "MDCOutlinedTextField"

How to use "MDCOutlinedTextField" in Swift

我需要如下图所示的文本字段

所以我正在使用 material-components-ios 框架,为此我在故事板中添加了文本字段并将其 class 名称命名为 MDCTextField 和代码

import UIKit

import MaterialComponents
class LoginVC: UIViewController {

@IBOutlet weak var emailTF: MDCTextField!

var textFieldControllerFloating: MDCTextInputControllerOutlined?

override func viewDidLoad() {
    super.viewDidLoad()
  //  setupEditText()
    
    textFieldControllerFloating = MDCTextInputControllerOutlined(textInput: emailTF)

    textFieldControllerFloating?.activeColor = UIColor.lightGray
    textFieldControllerFloating?.floatingPlaceholderActiveColor = UIColor.green
    textFieldControllerFloating?.normalColor = UIColor.lightGray
    textFieldControllerFloating?.inlinePlaceholderColor = UIColor.lightGray
    
}

此代码 o/p 完美但显示警告

警告:

'MDCTextField' is deprecated: MDCTextField and its associated classes are deprecated. Please use TextControls instead.

所以我添加了另一个框架 TextControls 在这里我添加了 pod 'MaterialComponents/TextControls+OutlinedTextFields' 并在 stroyboard 中添加了文本字段并将其 class 赋予 MDCOutlinedTextField 并添加了下面的代码

 import UIKit
 import MaterialComponents.MaterialTextControls_OutlinedTextFields

class LoginVC: UIViewController {

@IBOutlet weak var emailTF: MDCOutlinedTextField!
@IBOutlet weak var passwordTF: MDCOutlinedTextField!
}
}

但 o/p 如下所示:没有浮动占位符文本和较小的高度,为什么?我哪里错了..如何获得高度为 50 和浮动占位符边框如上图,请帮忙。

编辑: 我需要像这样的密码文本字段的眼睛按钮 如何处理 MDCOutlinedTextField?

按照 example 尝试设置 label.text 属性 那样

textField.label.text = "Phone number"

至于文本字段高度,您可以在 Interface Builder 中使用约束来设置它