UILabel “style”
April 14th, 2011
If, like me, you’d rather code your GUIs than spend time in Interface Builder, you’ll likely have a lot of code that looks like this:
// make a label UILabel* someLabel = [[UILabel alloc] init]; someLabel.font = [UIFont sytemFontOfSize:12]; someLabel.backgroundColor = [UIColor clearColor]; someLabel.textAlignment = UITextAlignmentCenter; someLabel.textColor = [UIColor whiteColor]; someLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.75]; someLabel.shadowOffset = CGSizeMake(0, -1); // and now make another label identical to that one, but with a bigger font UILabel* anotherLabel = [[UILabel alloc] init]; anotherLabel.font = [UIFont sytemFontOfSize:16]; anotherLabel.backgroundColor = [UIColor clearColor]; anotherLabel.textAlignment = UITextAlignmentCenter; anotherLabel.textColor = [UIColor whiteColor]; anotherLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.75]; anotherLabel.shadowOffset = CGSizeMake(0, -1); // and once more for yetAnotherLabel, this time with a tiny font UILabel* yetAnotherLabel = [[UILabel alloc] init]; yetAnotherLabel.font = [UIFont sytemFontOfSize:10]; yetAnotherLabel.backgroundColor = [UIColor clearColor]; yetAnotherLabel.textAlignment = UITextAlignmentCenter; yetAnotherLabel.textColor = [UIColor whiteColor]; yetAnotherLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.75]; yetAnotherLabel.shadowOffset = CGSizeMake(0, -1);
Lots of repetition, not much fun. So. I’ve been toying with this idea of UILabel styles. With a style, the code turns into this:
BBLabelStyle* style = [[BBLabelStyle alloc] init]; style.backgroundColor = [UIColor clearColor]; style.textAlignment = UITextAlignmentCenter; style.textColor = [UIColor whiteColor]; style.shadowColor = [UIColor colorWithWhite:0 alpha:0.75]; style.shadowOffset = CGSizeMake(0, -1); // make a label UILabel* someLabel = [[UILabel alloc] initWithLabelStyle:style]; someLabel.font = [UIFont sytemFontOfSize:12]; // and now make another label identical to that one, but with a bigger font UILabel* anotherLabel = [[UILabel alloc] initWithLabelStyle:style]; anotherLabel.font = [UIFont sytemFontOfSize:16]; // and once more for yetAnotherLabel, this time with a tiny font UILabel* yetAnotherLabel = [[UILabel alloc] initWithLabelStyle:style]; yetAnotherLabel.font = [UIFont sytemFontOfSize:10];
The labels are initialized with the properties defined by the style. All that’s left to do after that is specify the label’s font. What do you think? Useful? Another neat trick is applying multiple styles without them cancelling each other out. Here’s what I mean:
BBLabelStyle* centeredStyle = [[BBLabelStyle alloc] init];
BBLabelStyle* shadowedStyle = [[BBLabelStyle alloc] init];
centeredStyle.textAlignment = UITextAlignmentCenter;
shadowedStyle.shadowColor = [UIColor colorWithWhite:0 alpha:0.75];
shadowedStyle.shadowOffset = CGSizeMake(0, -1);
UILabel* label = [[UILabel alloc] initWithLabelStyles:
[NSArray arrayWithObjects:centered, shadowed, nil]];
In that example, the shadowedStyle only the shadow properties of offset and color, leaving the textAlignment property unaffected. I’ll put this up on github after a bit of testing. I set up a repository only this afternoon so I’m still getting used to it.
Looks great to me. I would definitely use this all the time.
Nice, I like this approach. I tend to trudge through interface builder in order to avoid this massive duplication of code, but I’ll give this a try to see if it makes implementing UI in code more bearable.
(Ignore the rest if you have git and github under control)
Re: github — I may be misunderstanding you, but it sounds like you’re going to put this in the same repository along with BBspinner. Might I suggest you create separate repos for the different projects? That way they can be forked and watched separately, etc.
Looking forward to giving this a try!
Hi Matt,
Why do you not make a factory function that generates UIlabels for you and sets the default settings that you want. That would be the *proper* way to do things.
cheers,
?:)
I’d just iterate over them and apply a specific style to each afterwards.