iOS 菜鸟在这里
当我向 ViewController 添加一个默认的
UITextField
时,它看起来像这样:
我的设计师希望我让
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];
斯威夫特
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];
斯威夫特:
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)
这将确保即使文本字段中没有文本,该行也是可见的。
为 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 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)
删除边框和添加下划线 - 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)
}
}
对于 Xcode 14.2
let textField = UITextField()
textField.defaultTextAttributes = [
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue,
NSAttributedString.Key.underlineColor: UIColor.red
]