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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

1111. appearing after print sequence - php -