使用MDCMultilineTextField具有边框和浮动占位符的UITextView

时间:2019-03-20 15:25:48

标签: ios swift uitextfield uitextview mdc-components

我正在尝试实现以下这些图像 enter image description here

在用户上编辑 enter image description here

这是我的代码

    @IBOutlet weak var commentTxtField: MDCMultilineTextField!  // Connected to storyboard

commentTxtField.textView?.delegate = self
        commentTxtField.textView?.frame = CGRect(x: (commentTxtField.textView?.frame.origin.x)!, y: (commentTxtField.textView?.frame.origin.y)!, width: (commentTxtField.textView?.frame.width)!, height: CGFloat(GenUtils.shared.getHeightForPercent(percent: 11.99)))
        commentTxtField.expandsOnOverflow = false
        commentTextFieldController = MDCTextInputControllerOutlinedTextArea(textInput: commentTxtField)
        commentTextFieldController?.placeholderText = "Comment"

        commentTextFieldController?.isFloatingEnabled = true
        commentTextFieldController!.characterCountMax = UInt(maxCharactersCount)
        commentTextFieldController?.characterCountViewMode = UITextField.ViewMode.never
        commentTextFieldController?.activeColor = UIColor.white.withAlphaComponent(0.6)
        commentTextFieldController?.normalColor = UIColor.white.withAlphaComponent(0.2)
        //        emailTextFieldController?.borderFillColor = UIColor.white
        commentTextFieldController?.floatingPlaceholderActiveColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 0.54)
        commentTextFieldController?.inlinePlaceholderColor = UIColor.white
        commentTextFieldController?.floatingPlaceholderNormalColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 0.54)
        commentTxtField.textColor = UIColor.white
        commentTextFieldController?.inlinePlaceholderFont = UIFont(name:"Avenir-Medium",size:16)

试图设置textviewframe,但不反映在屏幕上。而且也无法在边界线对齐上获得浮动占位符。我想念什么?

1 个答案:

答案 0 :(得分:0)

use **MDCTextField** as below:

class SomeVC: UIViewController, UITextFieldDelegate {

    var textFieldControllerFloating = MDCTextInputControllerUnderline()

    override func viewDidLoad() {
        let textFieldFloating = MDCTextField()
        self.view.addSubview(textFieldFloating)
        //place textfield where you want
        textFieldFloating.placeholder = "Some cool animating placeholder"
        textFieldFloating.delegate = self
        // This will animate the textfield's place holder
        textFieldControllerFloating = MDCTextInputControllerUnderline(textInput: textFieldFloating) 
    }
}

如果您也想使用大纲文本框,则可以使用“大纲”文本框 https://material.io/develop/ios/components/textfields/