3

我需要在我的快速项目中使用占位符实现大纲文本字段,例如 Material UI。我尝试用谷歌搜索相同但找不到​​任何东西。有谁知道如何快速实现它?参考图片:

在此处输入图像描述

套装

4

3 回答 3

5

使用这个吊舱,您可以获得相同的设计

  1. 使用情节UIView提要并设置约束
  2. 制作作为子类的类UIView并导入 podMaterialComponents.MaterialTextFieldsMaterialComponents.MaterialTextFields_ColorThemer

类 CustomOutlinedTxtField: UIView {

private var textFieldControllerFloating: MDCTextInputControllerOutlined!
var textField: MDCTextField!

@IBInspectable var placeHolder: String!
@IBInspectable var value: String!
@IBInspectable var primaryColor: UIColor! = .purple

override open func draw(_ rect: CGRect) {
    super.draw(rect)

    textField.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)

}
open override func awakeFromNib() {
    super.awakeFromNib()
    setUpProperty()
}
func setUpProperty() {
    //Change this properties to change the propperties of text
    textField = MDCTextField(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
    textField.placeholder = placeHolder
    textField.text = value

    //Change this properties to change the colors of border around text
    textFieldControllerFloating = MDCTextInputControllerOutlined(textInput: textField)

    textFieldControllerFloating.activeColor = primaryColor
    textFieldControllerFloating.floatingPlaceholderActiveColor = primaryColor
    textFieldControllerFloating.normalColor = UIColor.lightGray
    textFieldControllerFloating.inlinePlaceholderColor = UIColor.lightGray

    //Change this font to make borderRect bigger
    textFieldControllerFloating.inlinePlaceholderFont = UIFont.systemFont(ofSize: 14)
    textFieldControllerFloating.textInsets(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))

    self.addSubview(textField)
}

}

  1. 将该自定义类分配给UIView

在此处输入图像描述

结果

在此处输入图像描述

在此处输入图像描述

于 2020-02-20T12:54:11.393 回答
1

您可以使用材料组件:-

它将如下所示。

https://material.io/components/text-fields/ 在此处输入图像描述

Github 存储库 - https://github.com/material-components/material-components-ios

于 2020-02-20T12:44:04.130 回答
1

你也可以试试这个框架SkyFloatingLabelTextFieldTextFieldEffects或者TweeTextField

https://github.com/Skyscanner/SkyFloatingLabelTextField

https://github.com/oleghnidets/TweeTextField

https://github.com/raulriera/TextFieldEffects

于 2020-02-20T12:54:02.710 回答