uitextfield - Swift - code re-use -


i’m after advice on code re-use.

i have view controller has (at stage) 12 x labels , 12 x text fields.

for each of these labels , fields, there lines of code duplicated (see commented lines below).

i wondering best approach re-use lines of code in creation of labels , text fields, without re-writing them time.

i’ve looked extension’s, creating class , subclassing common lines of code, keep hitting walls.

i use class padding text fields , understand how works, can’t seem add other common attributes class. thanks

example:

let labela = uilabel() // labela.backgroundcolor = .clear // labela.widthanchor.constraint(equaltoconstant: 150).isactive = true // labela.font = labela.font.withsize(18) // labela.textalignment = .left labela.text = “this 1st label of 12“  let labelb = uilabel() // labelb.backgroundcolor = .clear // labelb.widthanchor.constraint(equaltoconstant: 150).isactive = true // labelb.font = labelb.font.withsize(18) // labelb.textalignment = .left labelb.text = “this 2nd label of 12“  let labelc = uilabel() // labelc.backgroundcolor = .clear // labelc.widthanchor.constraint(equaltoconstant: 150).isactive = true // labelc.font = labelc.font.withsize(18) // labelc.textalignment = .left labelc.text = “this 3rd label of 12“ 

** update **

thanks comments.

i re-using common lines of code adding func padding class.

unfortunately, text field padding no longer working.

class paddedtextfield: 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)     }      func createtext(with text: string) -> uitextfield {         let txtfield = uitextfield()         txtfield.backgroundcolor = .clear         txtfield.widthanchor.constraint(equaltoconstant: 250).isactive = true         txtfield.layer.borderwidth = 1         txtfield.layer.bordercolor = uicolor(r: 203, g: 203, b: 203).cgcolor         txtfield.layer.cornerradius = 5         txtfield.layer.maskstobounds = true         txtfield.placeholder = text         txtfield.isenabled = true         return txtfield     } }  

so, line of code works without field padding added...

let textfielda = paddedtextfield().createtext(with: "placeholder text...") 

... , works field padding, not re-using common lines of code.

let textfieldb = paddedtextfield() textfieldb.backgroundcolor = .clear textfieldb.widthanchor.constraint(equaltoconstant: 250).isactive = true textfieldb.layer.borderwidth = 1 textfieldb.layer.bordercolor = uicolor(r: 203, g: 203, b: 203).cgcolor textfieldb.layer.cornerradius = 5 textfieldb.layer.maskstobounds = true textfieldb.placeholder = "textfieldb placeholder text..." textfieldb.isenabled = true 

i not sure parts have wrong / don't understand. thanks.

creating uilabel subclass repeated properties effective , clean approach.

a second approach create factory

for example:

private class func factorylabel() -> uilabel {    let label = uilabel()    label.backgroundcolor = .clear    label.widthanchor.constraint(equaltoconstant: 150).isactive = true    label.font = labelc.font.withsize(18)    label.textalignment = .left     return label } 

...

let labelb = myclass.factorylabel() labelb.text = “this 2nd label of 12“ 

the example here class func, need call static func, i.e. name of class.


update

in update not using factory, creating subclass of uitextfield , inside subclass factoring uitextfield (i.e. not factoring subclass )

as have subclass having factory not necessary, change class this:

class paddedtextfield:uitextfield {     private let padding:uiedgeinsets = uiedgeinsets(top: 0, left: 5, bottom: 0, right: 5);      init(with text:string)     {         super.init(frame:cgrect.zero)         backgroundcolor = .clear         widthanchor.constraint(equaltoconstant:250).isactive = true         layer.borderwidth = 1         layer.bordercolor = uicolor(r: 203, g: 203, b: 203).cgcolor         layer.cornerradius = 5         layer.maskstobounds = true         placeholder = text         isenabled = true     }      required init?(coder adecoder:nscoder)     {         return nil     }      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)     } } 

and instantiate this:

let textfielda = paddedtextfield(with: "placeholder text...") 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -