ios - Text inset for UITextField -
i have seen post text inset uitextfield? , still not able figure out.
i want uitextfield
has 80.0 tall. want text 40.0 below top of uitextfield
, 20.0 tall, leaving 20.0 below text padding. current code is.
let textbounds: cgrect = cgrect(x: 0, y: 40, width: self.width, height: 20) self.editingrectforbounds(textbounds) self.textrectforbounds(textbounds)
the uitextfield
subclassed , believe text has 30.0 padding above , below , 20.0 tall or vertically centered within uitextfield
.
i have rewritten code use uitextfield
subclass:
class mytextfield: uitextfield { var textbounds: cgrect = cgrectzero override init(frame: cgrect) { super.init(frame: frame) textbounds = cgrectmake(0, 40, self.frame.width, 20) } required init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } override func textrectforbounds(bounds: cgrect) -> cgrect { return textbounds } override func editingrectforbounds(bounds: cgrect) -> cgrect { return textbounds } }
and added example of how might added view:
class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() let textfield = mytextfield(frame: cgrectmake(20, 64, 200, 50)) textfield.text = "hello world" // show border visualize frame. textfield.layer.bordercolor = uicolor.redcolor().cgcolor textfield.layer.borderwidth = 1 view.addsubview(textfield) } }
the uitextfield
subclass can added view controller file or separate file. choice.
Comments
Post a Comment