自定义UITextField,UILabel多行支持错误文本

时间:2018-05-21 09:14:57

标签: ios swift uitextfield

我想在其底部创建带有错误标签的自定义UITextField。我希望标签是多行的,我试过numberOfLines = 0。但它没有用。

这是我的课程片段

public class MyTextField: UITextField {
private let helperTextLabel: UILabel = {
    let label = UILabel()
    label.font = UIFont.systemFont(ofSize: 12.0, weight: UIFont.Weight.regular)
    label.textColor = helperTextColor
    label.numberOfLines = 0

    return label
}()

    public override init(frame: CGRect) {
    super.init(frame: frame)
    self.addSubview(errorTextLabel)
}

public required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.addSubview(errorTextLabel)
}

public override func layoutSubviews() {
    super.layoutSubviews()

    errorTextLabel.frame = CGRect(x: bounds.minX, y: bounds.maxY - 20, width: bounds.width, height: 20)
}
public override var intrinsicContentSize: CGSize {
    return CGSize(width: 240.0, height: 68.0)
}

public override func sizeThatFits(_ size: CGSize) -> CGSize {
    return intrinsicContentSize
}
}

我认为根本原因是因为我将高度设置为20,但如何根据errorTextLabel.text值动态设置高度?

2 个答案:

答案 0 :(得分:2)

您的标签尺寸固定。

不使用AutoLayout并在需要时提供文本字段和标签空间以扩展其大小。

就个人而言,如果创建这个特定的控件,我会在UIStackView中创建一个带有文本字段和标签的UIView。这样,如果在没有错误时隐藏标签,stackview将自动为您调整高度。然后,当您取消隐藏它时,视图将展开以适合两个控件。

一个基本的例子:

//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport

class LabelledTextView: UIView {

    private let label = UILabel()
    private let textfield = UITextField()
    private let stackView = UIStackView()


    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .white
        addSubview(stackView)

        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        stackView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        stackView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
        stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

        stackView.alignment = .leading
        stackView.axis = .vertical
        stackView.distribution = .fillEqually
        stackView.addArrangedSubview(textfield)
        stackView.addArrangedSubview(label)

        textfield.placeholder = "Please enter some text"
        label.numberOfLines = 0
        label.text = "Text did not pass validation, Text did not pass validation, Text did not pass validation, Text did not pass validation"
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

let labelledTextView = LabelledTextView(frame: CGRect(x: 50, y: 300, width: 300, height: 60))

let vc = UIViewController()
vc.view.addSubview(labelledTextView)
labelledTextView.translatesAutoresizingMaskIntoConstraints = false
labelledTextView.topAnchor.constraint(equalTo: vc.view.topAnchor).isActive = true
labelledTextView.leftAnchor.constraint(equalTo: vc.view.leftAnchor).isActive = true
labelledTextView.widthAnchor.constraint(equalToConstant: 300).isActive = true
labelledTextView.heightAnchor.constraint(greaterThanOrEqualToConstant: 60).isActive = true
PlaygroundPage.current.liveView = vc.view

答案 1 :(得分:0)

您需要使用sizeToFit()

    errorTextLabel.sizeToFit()