ios - How to programmatically add a label in a custom subview? -
i'm designing card game (cards against humanity) ios, there 2 types of cards, whites ones, , black ones. i've implemented cardview class inherits uiview, , 2 class, whitecardview , blackcardview both inherit cardview. guess, color change between two, still want 2 different view class 2 (even if same @ moment). so, have 4 questions that.
edit: mnage find out answer myself, last one, i'm lost.
1.i need advice on implementation, don't know if i'm doing right set colors of cards stored in cardview set in init of whitecardview. think have override 1 init, don't know one. if can confirm or correct did, great. thanks
here cardview code:
ib_designable @interface cardview : uiview -(void)customdrawui; -(void)setdata; @property (nonatomic) uicolor* ibinspectable secondarycolor; @property (nonatomic) uicolor* ibinspectable primarycolor; @property(nonatomic) uilabel* txtlabel; @end @implementation cardview -(void)customdrawui{ self.layer.cornerradius=5.0; self.backgroundcolor = self.primarycolor; } -(void)setdata{ self.txtlabel.text = @"some text"; } @end
and whitecardview code:
ib_designable @interface whitecardview : cardview @end @implementation whitecardview - (void)drawrect:(cgrect)rect { cgcontextref context = uigraphicsgetcurrentcontext(); cgrect myframe = self.bounds; cgcontextsetlinewidth(context, 1); cgrectinset(myframe, 5, 5); [self.secondarycolor set]; uirectframe(myframe); } -(id)initwithcoder:(nscoder *)adecoder{ self = [super initwithcoder:adecoder]; if (self) { self.primarycolor= [uicolor whitecolor]; self.secondarycolor= [uicolor blackcolor]; [super customdrawui]; [super setdata]; } return self; } @end
2.in interface builder (where correctly define custom class each of cards), views aren't live rendered, code in drawrect method whitecardview executed can see no change. , when run app, same result. white rectangle, without rounded angle. idea why there no changing in view?
edit: didn't put ibdesignable tag in right place, works !
3.is there way execute line: self.backgroundcolor = self.primarycolor;
in parent drawrect method ? because it's in both: white , blackcardview, , there way in parent class (i'll add common code 2 class, need way it). maybe it's possible in init cardview, it's not working @ moment, can't test it.
edit: find out, setbackground couldn't called drawrect, define new method in cardview class that. , call init methods.
4.the important part: how possible add label on each card (not via interface builder via cardview class), , able set content programmatically? defined property label, , set value have no idea how render it.
Comments
Post a Comment