ios - How can I properly group and minimize style code for UITextfields? -
i'm trying implement standard universal styling of text fields based strictly on login/sign fields.
so, designed them identical, think i'm reusing lot of code can condensed , maybe used in variable? i'm not sure how so.
the way works, i'm sure can done better this. i'm there's way minimize code better practice.
i'm still learning, want learn better practice in dev.
here's example of sign view & styling of fields:
class joinvc: uiviewcontroller, uitextfielddelegate {` @iboutlet weak var enteremailtextfield: uitextfield! @iboutlet weak var enterpasswordtextfield: uitextfield! @iboutlet weak var enternametextfield: uitextfield! override func viewdidload() { super.viewdidload() // field border corner + width self.enteremailtextfield.layer.cornerradius = 24.0 self.enteremailtextfield.layer.borderwidth = 1.5 self.enterpasswordtextfield.layer.cornerradius = 24.0 self.enterpasswordtextfield.layer.borderwidth = 1.5 self.enternametextfield.layer.cornerradius = 24.0 self.enternametextfield.layer.borderwidth = 1.5 // ... // field placeholder colorenter var placeholderenteremail = nsattributedstring(string: "enter email", attributes: [nsforegroundcolorattributename : uicolor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)]) var placeholderenterpass = nsattributedstring(string: "choose password", attributes: [nsforegroundcolorattributename : uicolor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)]) var placeholderentername = nsattributedstring(string: "choose username", attributes: [nsforegroundcolorattributename : uicolor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)]) enteremailtextfield.layer.sublayertransform = catransform3dmaketranslation(20, 0, 0); enterpasswordtextfield.layer.sublayertransform = catransform3dmaketranslation(20, 0, 0); enternametextfield.layer.sublayertransform = catransform3dmaketranslation(20, 0, 0); // ... // text field border color var bordercolor : uicolor = uicolor( red: 255, green: 255, blue:255, alpha: 0.8 ) self.enterpasswordtextfield.layer.bordercolor = bordercolor.cgcolor; enteremailtextfield.attributedplaceholder = placeholderenteremail; self.enteremailtextfield.layer.bordercolor = bordercolor.cgcolor; enterpasswordtextfield.attributedplaceholder = placeholderenterpass; self.enternametextfield.layer.bordercolor = bordercolor.cgcolor; enternametextfield.attributedplaceholder = placeholderentername; // ... } }
the way have solved before, if text fields in project are:
- always going have identical attributes,
- always going created storyboard
is subclass uitextfield
, apply attributes in -awakefromnib
:
class kbtextfield: uitextfield { let myattributes = [nsforegroundcolorattributename : uicolor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)] let mysublayertransform = catransform3dmaketranslation(20, 0, 0) let mybordercolor = uicolor( red: 255, green: 255, blue:255, alpha: 0.8 ) override func awakefromnib () { self.layer.sublayertransform = mysublayertransform self.layer.bordercolor = mybordercolor.cgcolor self.layer.cornerradius = 24.0 self.layer.borderwidth = 1.5 self.attributedplaceholder = nsattributedstring(string: self.placeholder!, attributes: myattributes) } }
then can set class right on storyboard (to kbtextfield
, in case) , take care of attributes automatically.
this way, can ensure kbtextfield
s in app identical, long create them through storyboard.
Comments
Post a Comment