Top Banner
CSS in iOS? - Keith Norman
25

Style vs. Content and Clean Theming in iOS

Jan 18, 2015

Download

Technology

Keith Norman

From Groupon Mobile Tech Talks on 2/27/2014 - a web developers approach to CSS-like separation between style and content in native iOS apps.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Style vs. Content and Clean Theming in iOS

CSS in iOS? - Keith Norman

Page 2: Style vs. Content and Clean Theming in iOS

Quick about me…

Page 3: Style vs. Content and Clean Theming in iOS
Page 4: Style vs. Content and Clean Theming in iOS
Page 5: Style vs. Content and Clean Theming in iOS
Page 6: Style vs. Content and Clean Theming in iOS
Page 7: Style vs. Content and Clean Theming in iOS
Page 8: Style vs. Content and Clean Theming in iOS

What’s good about CSS?

• loose coupling between style and content

• cleaner code

• makes design changes simple

• and redesigns feasible (iOS 7?)

• Come on, does this slide really need to exist?

Page 9: Style vs. Content and Clean Theming in iOS

Bad practice #1: putting style code in view controllers

textLabel.font = [UIFont fontWithName:kOpenSansFont size:17]; textLabel.textColor = RGBCOLOR(51, 51, 51); textLabel.backgroundColor = [UIColor clearColor];

<p style="font: 15px opensans; color: rgba(51, 51, 51, 1); background: none;"></p>

=

Page 10: Style vs. Content and Clean Theming in iOS

Bad practice #2: using Interface Builder

Page 11: Style vs. Content and Clean Theming in iOS

Demo

Page 12: Style vs. Content and Clean Theming in iOS

UIAppearance

[[UILabel appearance] setTextColor:[UIColor cyanColor]]; [[UILabel appearanceWhenContainedIn: [Subview class], nil] setTextColor:[UIColor redColor]]; [[UILabel appearanceWhenContainedIn: [SubSubview class], [Subview class], nil] setTextColor:[UIColor greenColor]];

Subview SubSubview

You can set these up in AppDelegate!

Page 13: Style vs. Content and Clean Theming in iOS

UINavigationBar barTintColorUISearchBar barTintColor

Page 14: Style vs. Content and Clean Theming in iOS

UINavigationBar tintColorNote: Not “officially” supported by UIAppearance

[[UINavigationBar appearance] setTintColor:[UIColor redColor]];

Page 15: Style vs. Content and Clean Theming in iOS

window tintColor trickles down to all subviews

- (void)tintColorDidChange { self.backgroundColor = self.tintColor; }

Page 16: Style vs. Content and Clean Theming in iOS

A bit about font label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];

https://gist.github.com/nuthatch/7594460

UIFontDescriptor *userFont = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody]; float userFontSize = [userFont pointSize]; UIFont *font = [UIFont fontWithName:@"OpenSans" size:userFontSize];

Custom font:

UIFontDescriptor *descriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleCaption1]; descriptor = [descriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold]; UIFont *font = [UIFont fontWithDescriptor:descriptor size:0]; [label setFont:font];

Bold font:

Page 17: Style vs. Content and Clean Theming in iOS

// PlayPauseButton.h !@interface PlayPauseButton : UIControl !@property (nonatomic, assign) PlayerState playState; !@property (nonatomic, strong) UIColor *controlButtonColor UI_APPEARANCE_SELECTOR; !@end

// PlayPauseButton.m !- (void)setControlButtonColor:(UIColor *)controlButtonColor { _controlButtonColor = controlButtonColor; [self setNeedsDisplay]; }

- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, self.controlButtonColor.CGColor); // ... draw icon ... // }

PaintCode App (for learning Core Graphics) http://www.paintcodeapp.com/

Core Graphics

Page 18: Style vs. Content and Clean Theming in iOS

Theme + “styleClass”

Bold

Not Bold

<p class="strong">Lost At Sea</p> <p>The Squirrel Nut Zippers</p>

Page 19: Style vs. Content and Clean Theming in iOS

- (void)awakeFromNib { self.albumName.styleClass = @"strongLabel"; }

[some magic happens]

and the text is bold

Page 20: Style vs. Content and Clean Theming in iOS

// UIView+StyleClass.h !@interface UIView (StyleClass) !@property (nonatomic, strong) NSString *styleClass; !@end

// UIView+StyleClass.m !@implementation UIView (StyleClass) !@dynamic styleClass; !- (void)setStyleClass:(NSString *)styleClass { NSString *selectorName = [@"style" stringByAppendingString:[NSString stringWithFormat:@“%@%@:", [[styleClass substringToIndex:1] uppercaseString], [styleClass substringFromIndex:1]]]; SEL sel = NSSelectorFromString(selectorName); if (class_getClassMethod([Theme class], sel) != NULL) { [Theme performSelector:sel withObject:self]; } } !@end

StyleClass Category

Page 21: Style vs. Content and Clean Theming in iOS

@implementation Theme !+ (void)styleStrongLabel:(UILabel *)label { UIFontDescriptor *descriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleCaption1]; descriptor = [descriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold]; UIFont *font = [UIFont fontWithDescriptor:descriptor size:0]; [label setFont:font]; } !@end

Theme class

Page 22: Style vs. Content and Clean Theming in iOS

menuItem.styleClass = @"myCollectionMenuItem";

menuItem.styleClass = @"recentlyPlayedMenuItem";

Page 23: Style vs. Content and Clean Theming in iOS

UIView+CSS (highly experimental)

@interface UIView (CSS) !@property (nonatomic, strong) CSSBorder *border UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) CSSBorder *borderTop UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) CSSBorder *borderBottom UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) CSSDropshadow *dropShadow UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) CSSBorderRadius *borderRadius UI_APPEARANCE_SELECTOR; !@end

CSSBorderRadius *borderRadius = [[CSSBorderRadius alloc] init]; borderRadius.radius = 15.0f; [[AlbumCard appearance] setBorderRadius:borderRadius];

Page 24: Style vs. Content and Clean Theming in iOS

Further reading Classy - https://github.com/cloudkite/Classy

UISS - https://github.com/robertwijas/UISS

WWDC 2012 #216 and 2013 #214!https://developer.apple.com/wwdc/videos/

Record Collection! https://github.com/keithnorm/RecordCollection

Page 25: Style vs. Content and Clean Theming in iOS