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

6139fd48daa26e786794598c4a6b546a?s=47 ninjinkun
February 12, 2014

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

6139fd48daa26e786794598c4a6b546a?s=128

ninjinkun

February 12, 2014
Tweet

Transcript

  1. 6*"QQFBSBODFΛ௨ͯ͠
 ΧελϜ7JFXΫϥεΛ࿔Δ ninjinkun

  2. ࠓ೔͓఻͍͑ͨ͜͠ͱ w ΧελϜ7JFXΫϥεΛ࡞Δͷʹ6*"QQFBSBODF͕ศར

  3. 6*"QQFBSBODFͱ͸ w 6*"QQFBSBODFΛ࣮૷͍ͯ͠ΔΦϒδΣΫτͷσϑΥ ϧτϓϩύςΟΛมߋͰ͖Δ࢓૊Έ w 6*7JFX͸6*"QQFBSBODFΛ࣮૷͍ͯ͠Δ w ࣗ෼Ͱఆٛͨ͠ϓϩύςΟ΋มߋͰ͖Δ

  4. ϘλϯΛ࡞Δ w ࿮ઢ෇͖ͷϘλϯΛ࢖͍͍ͨ w ৭ͷόϦΤʔγϣϯ͕͋Δ ྫ͑͹͜ΜͳγνϡΤʔγϣϯ

  5. ϘλϯΛ࡞Δ w ը૾Ͱ͕Μ͹Δ w #VUUPO#VJMEFSΛͭ͘Δ w YJC TUPSZCPBSEͰ࢖͑ͳ͍ w ϕʔεΫϥεΛ࡞ͬͯࢠΫϥεͰΦʔόʔϥΠυ͢Δ

    w ϕʔεΫϥεΛ࡞ͬͯࢠΫϥεΛ6*"QQFBSBODFͰૢ ࡞͢Δ ߟ͑ΒΕΔํ๏
  6. ϘλϯΛ࡞Δ 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
  7. @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
  8. ϘλϯΛ࡞Δ 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
  9. ϘλϯΛ࡞Δ 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
  10. @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
  11. ϝϦοτ w ࣮૷ͱςʔϚ͕෼཭Ͱ͖Δ w Ϙλϯ࣮૷Ϋϥε w ςʔϚΫϥε w )5.-$44ͷؔ܎ w

    1JYBUF /6* /JNCVT
  12. '"2 w ଟஈܧঝʹͳΔ͚Ͳʜ w ΦʔόʔϥΠυͯ͠ৼΔ෣͍Λม͍͑ͯΔ༁Ͱ͸ͳ͍ w ม͑Δͷ͸ॳظ஋ͷΈ w ܧঝͱݴ͏ΑΓ͸໊લΛม͍͑ͯΔ͚ͩ

  13. ·ͱΊ w ΧελϜ7JFXΛ6*"QQFBSBODF͔Βૢ࡞͢Δͱ
 ࣮૷ͱςʔϚ͕෼཭Ͱ͖ͯḿΔ