【问题标题】:Border width and placeholder in custom UITextField自定义 UITextField 中的边框宽度和占位符
【发布时间】:2019-01-13 03:05:18
【问题描述】:

我想做这个TextField

所以我写了这个类

class UnderLineTextField:UITextField{

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        let border = CALayer()
        let width = CGFloat(2.0)
        border.borderColor = UIColor.darkgray().cgColor
        border.frame = CGRect(x: 0, y: frame.size.height - width, width: frame.size.width, height: frame.size.height)

        textColor = UIColor.gold()
        backgroundColor = UIColor.clear

        border.borderWidth = width
        layer.addSublayer(border)
        layer.masksToBounds = true

        attributedPlaceholder = NSAttributedString(string: "",
                                                  attributes: [NSAttributedStringKey.foregroundColor: UIColor.darkgray()])}
}

但是有两个问题

  1. 下划线不等于TextField,如果我加宽width: frame.size.width + 500,问题可以解决,但为什么呢?我应该传递给width: 的正确值是多少?

  2. attributedPlaceholder将替换我在xib中设置的占位符,如何在不添加空字符串的情况下更改占位符?

【问题讨论】:

标签: ios swift uitextfield


【解决方案1】:
  1. 下划线不等于TextField,如果我加多width宽度:frame.size.width + 500,问题可以解决,但是为什么呢?我应该传递给宽度的正确值是多少:?

您应该将绘图下划线代码放在draw(_ rect: CGRect) 而不是init。因为你使用autolayout,所以你在init中得到的宽度与你在运行应用程序时看到的width不同。

  1. attributedPlaceholder 将替换我在xib中设置的占位符,如何在不添加空字符串的情况下更改占位符?

您也可以在 draw(_ rect: CGRect) 中的占位符基础上重新绘制属性占位符

attributedPlaceholder = NSAttributedString(
    string: self.placeholder,
    attributes: [NSAttributedStringKey.foregroundColor: UIColor.darkgray()]
)

【讨论】:

  • draw(_ rect: CGRect) 有效,但占位符无效,我尝试将attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: [NSAttributedStringKey.foregroundColor: UIColor.darkgray()]) 放入initdrawself.placeholder! 都为空
  • 对不起,我的错误,现在可以了,我放入init,这是最简单的解决方案
【解决方案2】:

看来你的代码没问题。

使用以下代码设置占位符:

class UnderLineTextField:UITextField{

    @IBInspectable var borderColor: UIColor = UIColor.white {
        didSet {
            layer.borderColor = borderColor.cgColor

        }
    }
    @IBInspectable var placeHolderText: String = "default place holder" {
        didSet {
            attributedPlaceholder = NSAttributedString(string: placeHolderText,
                                                       attributes: [NSAttributedStringKey.foregroundColor: UIColor.darkGray])}

        }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        let border = CALayer()
        let width = CGFloat(2.0)
        border.borderColor = UIColor.darkGray.cgColor
        border.frame = CGRect(x: 0, y: frame.size.height - width, width: frame.size.width, height: frame.size.height)

        textColor = UIColor.lightGray
        backgroundColor = UIColor.clear

        border.borderWidth = width
        layer.addSublayer(border)
        layer.masksToBounds = true

        attributedPlaceholder = NSAttributedString(string: placeHolderText,
                                                   attributes: [NSAttributedStringKey.foregroundColor: UIColor.darkGray])}
}

并在情节提要中设置占位符:

结果如下所示:

【讨论】:

  • 我觉得这也是一个很好的答案,但我还是喜欢最简单的一个
【解决方案3】:

UITextField 下划线使用这个...

let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.gray.cgColor
border.frame = CGRect(x: 0, y: textField.frame.size.height - width, width:  textField.frame.size.width, height: textField.frame.size.height)

border.borderWidth = width
textField.layer.addSublayer(border)
textField.layer.masksToBounds = true

【讨论】:

    【解决方案4】:

    嘿,已经有很多答案了:),让我们看看这个详细的答案是否可以帮助您实现更多目标: 如果可能的话,我更喜欢使用扩展而不是继承。

    extension UITextField {
    
    func setTextColor(_ color: UIColor, font: UIFont) {
    
        self.textColor = color
        self.font = font
    }
    
    func setBottomBorder(with color: UIColor, width: CGFloat) {
        self.borderStyle = .none
        self.layer.backgroundColor = UIColor.white.cgColor // bg color of your choice
    
        self.layer.masksToBounds = false
        self.layer.shadowColor = color.cgColor
        self.layer.shadowOffset = CGSize(width: 0.0, height: width)
        self.layer.shadowOpacity = 1.0
        self.layer.shadowRadius = 0.0
    }
    
    
    func setPlaceHolderAttributes(placeHolderText : String, colour : UIColor , font : UIFont){
    
        self.attributedPlaceholder = NSAttributedString(string:placeHolderText, attributes:[NSAttributedStringKey.foregroundColor: colour , NSAttributedStringKey.font : font])
    }
    

    }

    //MARK: 给文本插入

    class MyTextField: UITextField {
    
    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect.init(x:10, y:2, width:bounds.width-20, height:bounds.height - 4)
    }
    
    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return textRect(forBounds: bounds)
    }
    

    }

    而且我更喜欢从 Code 配置 UI,它会加快您的编码速度并实现从一个地方进行项目明智的更改,因此您可以通过以下代码来完成:

     let font: UIFont = UIFont.systemFont(ofSize: 14.0)
        let placeHolderTextColor: UIColor = UIColor.brown
        let textColor: UIColor = UIColor.darkText
    
        nameTextField.setBottomBorder(with: UIColor.darkGray, width: 1.0)
        passwordTextField.setBottomBorder(with: UIColor.darkGray, width: 1.0)
    
        nameTextField.setPlaceHolderAttributes(placeHolderText: "Name", colour: placeHolderTextColor, font: font)
        nameTextField.setTextColor(textColor, font: font)
    
        passwordTextField.setPlaceHolderAttributes(placeHolderText: "Password", colour: placeHolderTextColor, font: font)
        passwordTextField.setTextColor(textColor, font: font)
    

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      相关资源
      最近更新 更多