Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UIAppearanceを通して
カスタムViewクラスを弄る

ninjinkun
February 12, 2014

 UIAppearanceを通して
カスタムViewクラスを弄る

ninjinkun

February 12, 2014
Tweet

More Decks by ninjinkun

Other Decks in Technology

Transcript

  1. ϘλϯΛ࡞Δ w ࿮෇͖ϘλϯͷϕʔεΫϥεΛ࡞Δ ϕʔεΫϥεΛ࡞ͬͯࢠΫϥεΛ6*"QQFBSBODFͰૢ࡞͢Δ @interface NJKBaseButton : UIButton ! @property

    (nonatomic) UIColor *borderColor UI_APPEARANCE_SELECTOR; @property (nonatomic) CGFloat borderWidth UI_APPEARANCE_SELECTOR; @property (nonatomic) CGFloat cornerRadius UI_APPEARANCE_SELECTOR; ! @end
  2. @implementation NJKBaseButton ! - (id)initWithFrame:(CGRect)frame { … } ! -

    (id)initWithCoder:(NSCoder *)aDecoder { … } ! - (void)setupViews { self.layer.masksToBounds = YES; ! // avoid storybord overriding UIColor *color = [[[self class] appearance] titleColorForState:UIControlStateNormal]; if (color) { [self setTitleColor:color forState:UIControlStateNormal]; } } ! - (void)setBorderColor:(UIColor *)borderColor { _borderColor = borderColor; self.layer.borderColor = borderColor.CGColor; } ! - (void)setBorderWidth:(CGFloat)borderWidth { _borderWidth = borderWidth; self.layer.borderWidth = borderWidth; } ! - (void)setCornerRadius:(CGFloat)cornerRadius { _cornerRadius = cornerRadius; self.layer.cornerRadius = cornerRadius; } ! @end
  3. ϘλϯΛ࡞Δ w ϕʔεΫϥεΛܧঝͯ͠৭͖ͭϘλϯͷΫϥεΛ࡞Δ w ΫϥεΛ࡞Δ͚ͩͰ࣮૷͸ۭ ϕʔεΫϥεΛ࡞ͬͯࢠΫϥεΛ6*"QQFBSBODFͰૢ࡞͢Δ @interface NJKBlueButton : NJKBaseButton

    @end ! @interface NJKBlueLineButton : NJKBaseButton @end ! @interface NJKGreenButton : NJKBaseButton @end ! ! @implementation NJKBlueButton @end ! @implementation NJKBlueLineButton @end ! @implementation NJKGreenButton @end
  4. ϘλϯΛ࡞Δ w ৭ͷఆٛ͸6*"QQFBSBODFΛ௨ͯ͠ผͷίʔυͰࢦఆ ͢Δ ϕʔεΫϥεΛ࡞ͬͯࢠΫϥεΛ6*"QQFBSBODFͰૢ࡞͢Δ @implementation NJKTheme ! + (void)initialize

    { [[NJKBaseButton appearance] setCornerRadius:4.0]; ! ! [[NJKBlueButton appearance] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; ! ! [[NJKBlueLineButton appearance] setBorderWidth:1.0]; [[NJKBlueLineButton appearance] setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [[NJKBlueLineButton appearance] setBorderColor:[UIColor blueColor]]; } ! @end
  5. @implementation NJKTheme ! + (void)initialize { ! [[NJKBaseButton appearance] setCornerRadius:4.0];

    ! ! [[NJKBlueButton appearance] setTitleColor:[UIColor colorWithString:@"#ffffff"] forState:UIControlStateNormal]; [[NJKBlueButton appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithString:@"#3498DB"]] forState:UIControlStateNormal]; [[NJKBlueButton appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithString:@"#2980B9"]] forState:UIControlStateHighlighted]; ! ! [[NJKBlueLineButton appearance] setBorderWidth:1.0]; [[NJKBlueLineButton appearance] setTitleColor:[UIColor colorWithString:@"#3498DB"] forState:UIControlStateNormal]; [[NJKBlueLineButton appearance] setTitleColor:[UIColor colorWithString:@"#ffffff"] forState:UIControlStateHighlighted]; [[NJKBlueLineButton appearance] setBorderColor:[UIColor colorWithString:@"#3498DB"]]; [[NJKBlueLineButton appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithString:@"#3498DB"]] forState:UIControlStateHighlighted]; ! } ! @end