在 Ios 中创建只有底线的文本字段
Creating a textfield with only bottom Line in Ios
可以在 Ios 中创建一个仅显示底部边框线而不显示上边框线和边线的文本字段。如果是,我该如何实现这个
是的,这是可能的。操作方法如下:
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;
希望对您有所帮助!!
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
}
用法:
self.addBottomLineToTextField(usernameTextField)
self.addBottomLineToTextField(passwordTextField)
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
}
}
这样称呼它:
yourtextFieldName.useUnderline()
只需在 Ios
中使用 CALayer 到只有底线的文本字段
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];
更新为 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
}
}
可以在 Ios 中创建一个仅显示底部边框线而不显示上边框线和边线的文本字段。如果是,我该如何实现这个
是的,这是可能的。操作方法如下:
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;
希望对您有所帮助!!
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
}
用法:
self.addBottomLineToTextField(usernameTextField)
self.addBottomLineToTextField(passwordTextField)
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
}
}
这样称呼它:
yourtextFieldName.useUnderline()
只需在 Ios
中使用 CALayer 到只有底线的文本字段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];
更新为 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
}
}