ios - Create space at the beginning of a UITextField -
i want leave bit of space @ beginning of uitextfield, here: add lefthand margin uitextfield
but don't know how swift.
this using right now:
class textfield: uitextfield { let padding = uiedgeinsets(top: 0, left: 5, bottom: 0, right: 5); override func textrectforbounds(bounds: cgrect) -> cgrect { return uiedgeinsetsinsetrect(bounds, padding) } override func placeholderrectforbounds(bounds: cgrect) -> cgrect { return uiedgeinsetsinsetrect(bounds, padding) } override func editingrectforbounds(bounds: cgrect) -> cgrect { return uiedgeinsetsinsetrect(bounds, padding) } }
for swift 3:
class textfield: uitextfield { let padding = uiedgeinsets(top: 0, left: 5, bottom: 0, right: 5); override func textrect(forbounds bounds: cgrect) -> cgrect { return uiedgeinsetsinsetrect(bounds, padding) } override func placeholderrect(forbounds bounds: cgrect) -> cgrect { return uiedgeinsetsinsetrect(bounds, padding) } override func editingrect(forbounds bounds: cgrect) -> cgrect { return uiedgeinsetsinsetrect(bounds, padding) } }
i never set other padding can tweak. class doesn't take care of rightview , leftview on textfield. if want handle correctly can use (example in objc , needed rightview:
- (cgrect)textrectforbounds:(cgrect)bounds { cgrect paddedrect = uiedgeinsetsinsetrect(bounds, self.insets); if (self.rightviewmode == uitextfieldviewmodealways || self.rightviewmode == uitextfieldviewmodeunlessediting) { return [self adjustrectwithwidthrightview:paddedrect]; } return paddedrect; } - (cgrect)placeholderrectforbounds:(cgrect)bounds { cgrect paddedrect = uiedgeinsetsinsetrect(bounds, self.insets); if (self.rightviewmode == uitextfieldviewmodealways || self.rightviewmode == uitextfieldviewmodeunlessediting) { return [self adjustrectwithwidthrightview:paddedrect]; } return paddedrect; } - (cgrect)editingrectforbounds:(cgrect)bounds { cgrect paddedrect = uiedgeinsetsinsetrect(bounds, self.insets); if (self.rightviewmode == uitextfieldviewmodealways || self.rightviewmode == uitextfieldviewmodewhileediting) { return [self adjustrectwithwidthrightview:paddedrect]; } return paddedrect; } - (cgrect)adjustrectwithwidthrightview:(cgrect)bounds { cgrect paddedrect = bounds; paddedrect.size.width -= cgrectgetwidth(self.rightview.frame); return paddedrect; }
Comments
Post a Comment