简体   繁体   English

UIcollectionViewCell中的UITextField的单个边框(swift 3 xcode)

[英]single border for UITextField within a UIcollectionViewCell (swift 3 xcode)

I'm trying to apply a single bottom border to a textField that sits within one of my collectionViewCells. 我正在尝试将单个底部边框应用于位于我的collectionViewCells之一内的textField。 Here is the code: 这是代码:

class AddressCell: UICollectionViewCell {


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


func basicTextField(placeHolderString: String) -> UITextField {
    let textField = UITextField()
    textField.font = UIFont.boldSystemFont(ofSize: 12)
    textField.attributedPlaceholder = NSAttributedString(string: placeHolderString, attributes:[NSForegroundColorAttributeName: UIColor.lightGray, NSFontAttributeName: UIFont.boldSystemFont(ofSize: 12)])
    textField.backgroundColor = UIColor.white
    textField.translatesAutoresizingMaskIntoConstraints = false
    return textField
}

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

func setupViews() {
    backgroundColor = UIColor.white
    layer.addBorder(edge: UIRectEdge.bottom, color: .black, thickness: 0.5)

    let streetTextfield = basicTextField(placeHolderString: "street")

    addSubview(streetTextfield)
}

} }

I am using an extension that enables me to apply a single border, which has worked great so far: 我正在使用扩展程序,使我可以应用单个边框,到目前为止效果很好:

extension CALayer {

func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {

    let border = CALayer()

    switch edge {
    case UIRectEdge.top:
        border.frame = CGRect.init(x: 0, y: 0, width: frame.width, height: thickness)
        break
    case UIRectEdge.bottom:
        border.frame = CGRect.init(x: 0, y: frame.height - thickness, width: frame.width, height: thickness)
        break
    case UIRectEdge.left:
        border.frame = CGRect.init(x: 0, y: 0, width: thickness, height: frame.height)
        break
    case UIRectEdge.right:
        border.frame = CGRect.init(x: frame.width - thickness, y: 0, width: thickness, height: frame.height)
        break
    default:
        break
    }

    border.backgroundColor = color.cgColor;

    self.addSublayer(border)
}

} }

When i simply add a borderWidth to the textfield like this: 当我只是像这样在文本字段中添加borderWidth时:

textField.layer.borderWidth = 0.5

I get a border and it renders fine. 我得到一个边框,并且效果很好。 However, when i apply the extension to add a bottom border, like this: 但是,当我应用扩展名添加底部边框时,如下所示:

textField.layer.addBorder(edge: UIRectEdge.bottom, color: .black, thickness: 0.5)

the border doesn't apply for some reason. 边框由于某些原因不适用。

I'm not sure about the extension, but I have a working solution for a TextField with a bottom border. 我不确定扩展名,但是对于底部边框的TextField我有一个可行的解决方案。

basically create a class BottomBorderedTextField subclassing UITextField and insert the following code: 基本上创建一个类BottomBorderedTextField子类化UITextField并插入以下代码:

class BottomBorderedTextField: UITextField {

    override func draw(_ rect: CGRect) {

        super.draw(rect)

        //this is the color of the bottom border. Change to whatever you what
        let color: UIColor = UIColor.rgb(red: 230, green: 230, blue: 230)

        let bottomBorder = CALayer()

        bottomBorder.frame = CGRect(x: 0, y: bounds.size.height - 1, width: bounds.size.width, height: 2)

        bottomBorder.backgroundColor = color.cgColor

        self.layer.addSublayer(bottomBorder)

    }
}

Then on your basicTextField function set the return type to the BottomBorderedTextField class you just made. 然后在basicTextField函数上,将返回类型设置为刚创建的BottomBorderedTextField类。 so your new code will look like: 因此您的新代码将如下所示:

func basicTextField(placeHolderString: String) -> BottomBorderedTextField {
    let textField = BottomBorderedTextField()
    textField.font = UIFont.boldSystemFont(ofSize: 12)
    textField.attributedPlaceholder = NSAttributedString(string: placeHolderString, attributes:[NSForegroundColorAttributeName: UIColor.lightGray, NSFontAttributeName: UIFont.boldSystemFont(ofSize: 12)])
    textField.backgroundColor = UIColor.white
    textField.translatesAutoresizingMaskIntoConstraints = false
    return textField
}

I wrote a UIView extension that accomplishes the same thing: 我写了一个UIView扩展来完成同样的事情:

extension UIView {

    func addBottomBorder(width: CGFloat, color: UIColor, alpha: CGFloat) {
        let border = CALayer()
        let width = width
        border.borderColor = color.withAlphaComponent(alpha).cgColor
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  self.frame.size.width, height: self.frame.size.height)
        border.borderWidth = width
        self.layer.addSublayer(border)
        self.layer.masksToBounds = true
    }
}

Usage: 用法:

    usernameField.addBottomBorder(width: 2.0, color: UIColor.white, alpha: 0.5)

Also, seems within setupViews() you're calling: 此外,似乎在setupViews()您正在调用:

layer.addBorder... //this is being called on the collection view cells layer

As opposed to: 相对于:

streetTextfield.layer.addBorder...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM