如何创建带下划线的 UITextField
How to create underlined UITextField
iOS菜鸟在这里
当我将默认 UITextField
添加到 ViewController 时,它看起来像这样:
我的设计师希望我将 UITextField
做成这样(即背景是透明的,只有一个下划线:
我该怎么做?应该向我的设计师询问图像以设置为 UITextField
的背景。图像应该是什么样的?图像应该=蓝色背景+下划线(减去我猜的数字)
对吗?
更新:
根据下面的方向(来自@Ashish Kakkad),将 Swift 中的此代码添加到 ViewDidLoad:
var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, view.frame.height - 1, view.frame.width, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
tf_editPassword.borderStyle = UITextBorderStyle.None
tf_editPassword.layer.addSublayer(bottomLine)
这几乎行得通,但问题是所有侧边都消失了,我只想保留底线。
试试这个代码:
Obj-C
CALayer *bottomLine = [CALayer layer];
bottomLine.frame = CGRectMake(0.0f, self.frame.size.height - 1, self.frame.size.width, 1.0f);
bottomLine.backgroundColor = [UIColor whiteColor].CGColor;
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField.layer addSublayer:bottomLine];
Swift
var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, view.frame.height - 1, view.frame.width, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
myTextField.borderStyle = UITextBorderStyle.None
myTextField.layer.addSublayer(bottomLine)
希望对您有所帮助
如果您使用的是自动布局,请尝试在 viewDidLayoutSubviews
方法中执行此代码。
@Ashish Kakkad 发布了正确的代码,但它可能不适用于@user1406716 的原因是当文本字段中没有文本时,框架的高度和宽度为 0,所以也许您可以尝试以下 :
Obj-C:
CALayer *bottomLine = [CALayer layer];
bottomLine.frame = CGRectMake(0.0f, 75 - 1, 300, 1.0f);
bottomLine.backgroundColor = [UIColor whiteColor].CGColor;
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField.layer addSublayer:bottomLine];
Swift :
var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, 75 - 1, 300, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
myTextField.borderStyle = UITextBorderStyle.None
myTextField.layer.addSublayer(bottomLine)
这将确保该行即使在文本字段中没有文本时也可见。
要用 Swift 3 更新:
let bottomLine = CALayer()
bottomLine.frame = CGRect(origin: CGPoint(x: 0, y:first.frame.height - 1), size: CGSize(width: first.frame.width, height: 1))
bottomLine.backgroundColor = UIColor.white.cgColor
first.borderStyle = UITextBorderStyle.none
first.layer.addSublayer(bottomLine)
将此扩展用于 UITextField
extension UITextField {
func setUnderLine() {
let border = CALayer()
let width = CGFloat(0.5)
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width - 10, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
在ViewDidLoad()
中调用上述函数并显示带下划线的文本框。
myTextField.setUnderLine()
删除边框并添加下划线 - Swift 5:
extension UITextField {
func addUnderLine () {
let bottomLine = CALayer()
bottomLine.frame = CGRect(x: 0.0, y: self.bounds.height + 3, width: self.bounds.width, height: 1.5)
bottomLine.backgroundColor = UIColor.lightGray.cgColor
self.borderStyle = UITextField.BorderStyle.none
self.layer.addSublayer(bottomLine)
}
}
iOS菜鸟在这里
当我将默认 UITextField
添加到 ViewController 时,它看起来像这样:
我的设计师希望我将 UITextField
做成这样(即背景是透明的,只有一个下划线:
我该怎么做?应该向我的设计师询问图像以设置为 UITextField
的背景。图像应该是什么样的?图像应该=蓝色背景+下划线(减去我猜的数字)
对吗?
更新: 根据下面的方向(来自@Ashish Kakkad),将 Swift 中的此代码添加到 ViewDidLoad:
var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, view.frame.height - 1, view.frame.width, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
tf_editPassword.borderStyle = UITextBorderStyle.None
tf_editPassword.layer.addSublayer(bottomLine)
这几乎行得通,但问题是所有侧边都消失了,我只想保留底线。
试试这个代码:
Obj-C
CALayer *bottomLine = [CALayer layer];
bottomLine.frame = CGRectMake(0.0f, self.frame.size.height - 1, self.frame.size.width, 1.0f);
bottomLine.backgroundColor = [UIColor whiteColor].CGColor;
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField.layer addSublayer:bottomLine];
Swift
var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, view.frame.height - 1, view.frame.width, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
myTextField.borderStyle = UITextBorderStyle.None
myTextField.layer.addSublayer(bottomLine)
希望对您有所帮助
如果您使用的是自动布局,请尝试在 viewDidLayoutSubviews
方法中执行此代码。
@Ashish Kakkad 发布了正确的代码,但它可能不适用于@user1406716 的原因是当文本字段中没有文本时,框架的高度和宽度为 0,所以也许您可以尝试以下 :
Obj-C:
CALayer *bottomLine = [CALayer layer];
bottomLine.frame = CGRectMake(0.0f, 75 - 1, 300, 1.0f);
bottomLine.backgroundColor = [UIColor whiteColor].CGColor;
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField.layer addSublayer:bottomLine];
Swift :
var bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, 75 - 1, 300, 1.0)
bottomLine.backgroundColor = UIColor.whiteColor().CGColor
myTextField.borderStyle = UITextBorderStyle.None
myTextField.layer.addSublayer(bottomLine)
这将确保该行即使在文本字段中没有文本时也可见。
要用 Swift 3 更新:
let bottomLine = CALayer()
bottomLine.frame = CGRect(origin: CGPoint(x: 0, y:first.frame.height - 1), size: CGSize(width: first.frame.width, height: 1))
bottomLine.backgroundColor = UIColor.white.cgColor
first.borderStyle = UITextBorderStyle.none
first.layer.addSublayer(bottomLine)
将此扩展用于 UITextField
extension UITextField {
func setUnderLine() {
let border = CALayer()
let width = CGFloat(0.5)
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width - 10, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
在ViewDidLoad()
中调用上述函数并显示带下划线的文本框。
myTextField.setUnderLine()
删除边框并添加下划线 - Swift 5:
extension UITextField {
func addUnderLine () {
let bottomLine = CALayer()
bottomLine.frame = CGRect(x: 0.0, y: self.bounds.height + 3, width: self.bounds.width, height: 1.5)
bottomLine.backgroundColor = UIColor.lightGray.cgColor
self.borderStyle = UITextField.BorderStyle.none
self.layer.addSublayer(bottomLine)
}
}