objective-c 在 Ios 中创建一个只有底线的文本字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29428402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a textfield with only bottom Line in Ios
提问by Junior Bill gates
Is is possible to create a Textfield in Ios which shows only bottom border line and not upper and side lines. if Yes, how can i implement this
是否可以在 Ios 中创建一个仅显示底部边框线而不显示顶部和侧边线的文本字段。如果是,我该如何实施
回答by
Yes it is possible. Here is how to do it:
对的,这是可能的。这是如何做到的:
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor darkGrayColor].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
Hope this helps!!
希望这可以帮助!!
回答by mobilecat
Updated for Swift 3.0
为 Swift 3.0 更新
extension UITextField {
func useUnderline() {
let border = CALayer()
let borderWidth = CGFloat(1.0)
border.borderColor = UIColor.lightGray.cgColor
border.frame = CGRect(origin: CGPoint(x: 0,y :self.frame.size.height - borderWidth), size: CGSize(width: self.frame.size.width, height: self.frame.size.height))
border.borderWidth = borderWidth
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
回答by Adrian
Swift:
迅速:
extension UITextField {
func useUnderline() {
let border = CALayer()
let borderWidth = CGFloat(1.0)
border.borderColor = UIColor.blackColor().CGColor
border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, self.frame.size.height)
border.borderWidth = borderWidth
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
Call it like this:
像这样调用它:
yourtextFieldName.useUnderline()
回答by Jigar Darji
Simply use CALayer to textfield with only bottom Line in Ios
只需使用 CALayer 到文本字段,在 Ios 中只有底线
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, self.passwordField.frame.size.height - 1, self.passwordField.frame.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
[self.passwordField.layer addSublayer:bottomBorder];
回答by Babac
User4645956's solution in Swift
User4645956 在 Swift 中的解决方案
private func addBottomLineToTextField(textField : UITextField) {
let border = CALayer()
let borderWidth = CGFloat(1.0)
border.borderColor = UIColor.whiteColor().CGColor
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height)
border.borderWidth = borderWidth
textField.layer.addSublayer(border)
textField.layer.masksToBounds = true
}
Usage:
用法:
self.addBottomLineToTextField(usernameTextField)
self.addBottomLineToTextField(passwordTextField)

