UIcollectionViewCell中的UITextField的单边界(swift 3 xcode)(single border for UITextField within a UIcollec

编程入门 行业动态 更新时间:2024-10-10 21:24:17
UIcollectionViewCell中的UITextField的单边界(swift 3 xcode)(single border for UITextField within a UIcollectionViewCell (swift 3 xcode))

我试图将一个底部边框应用到位于我的一个collectionViewCells中的textField。 代码如下:

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) }

}

我正在使用一个扩展,使我能够应用单一的边界,迄今为止效果很好:

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) }

}

当我简单地像这样添加一个borderWidth到文本框:

textField.layer.borderWidth = 0.5

我得到一个边框,它呈现很好。 但是,当我应用扩展来添加底部边框,如下所示:

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

由于某种原因,边界不适用。

I'm trying to apply a single bottom border to a textField that sits within one of my collectionViewCells. 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:

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.

最满意答案

我不确定扩展名,但是我有一个带有底部边框的TextField的工作解决方案。

基本上创建一个类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) } }

然后,在basicTextField函数中,将返回类型设置为您刚创建的BottomBorderedTextField类。 所以你的新代码将如下所示:

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'm not sure about the extension, but I have a working solution for a TextField with a bottom border.

basically create a class BottomBorderedTextField subclassing UITextField and insert the following code:

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. 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 }

更多推荐

本文发布于:2023-07-27 15:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:边界   UITextField   UIcollectionViewCell   swift   border

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!