【发布时间】:2019-01-13 03:05:18
【问题描述】:
我想做这个TextField
所以我写了这个类
class UnderLineTextField:UITextField{
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor.darkgray().cgColor
border.frame = CGRect(x: 0, y: frame.size.height - width, width: frame.size.width, height: frame.size.height)
textColor = UIColor.gold()
backgroundColor = UIColor.clear
border.borderWidth = width
layer.addSublayer(border)
layer.masksToBounds = true
attributedPlaceholder = NSAttributedString(string: "",
attributes: [NSAttributedStringKey.foregroundColor: UIColor.darkgray()])}
}
但是有两个问题
下划线不等于TextField,如果我加宽
width: frame.size.width + 500,问题可以解决,但为什么呢?我应该传递给width:的正确值是多少?attributedPlaceholder将替换我在xib中设置的占位符,如何在不添加空字符串的情况下更改占位符?
【问题讨论】:
-
使用 ibinspectable 作为占位符 ref :medium.com/anantha-krishnan-k-g/…
标签: ios swift uitextfield