我试图将一个底部边框应用到位于我的一个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.5I 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 }更多推荐
发布评论