objective c - How to customising subclass implementation at runtime in iOS? -


mytextfield uitextfield subclass have margin in textfield.

@interface mytextfield : uitextfield @property (nonatomic, assign) bool enablemargin; - (instancetype) initwithmarginenable:(bool)enable; @end  @implementation mytextfield - (cgrect)textrectforbounds:(cgrect)bounds {      if(self.enablemargin) return;      return cgrectinset(bounds, 32.5f, 0); }  - (cgrect)editingrectforbounds:(cgrect)bounds {     return [self textrectforbounds:bounds]; }  - (instancetype) initwithmarginenable:(bool)enable {     self = [super init];     if(self) {         self.enablemargin = enable;     }     return self; } @end 

this working fine !

mytextfield *txt = [[mytextfield alloc] init]; 

but @ point in app, require not have margin, however, keep continuity , reason still have use mytextfield throughout app.

this doesn't !

mytextfield *txt = [[mytextfield alloc] initwithmarginenable:yes]; 

but in personal investigation realized textrectforbounds: method call before mytextfield's init.

how make (or check it) don't want margin or not? i've tried custom init method still calls textrectforbounds:.

and yes, app supports ios7 > advice/suggestion/answer should based on condition :)

you have call setneedsdisplay after setting enablemargin property. don't need make separate init, as:

 @implementation mytextfield  - (cgrect)textrectforbounds:(cgrect)bounds {      if(self.enablemargin) return cgrectinset(bounds, 0, 0);;      return cgrectinset(bounds, 32.5f, 0); }  - (cgrect)editingrectforbounds:(cgrect)bounds {     return [self textrectforbounds:bounds]; }  -(void)setenablemargin:(bool)enablemargin {     _enablemargin = enablemargin;     [self setneedsdisplay]; }  @end 

to use it, have call:

 mytextfield *mytext = [[mytextfield alloc] init];  mytext.frame = // whatever frame  mytext.enablemargin = yes; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -