Hacker News new | past | comments | ask | show | jobs | submit login

I can see it in both code and interface builder. Doing it in code on iOS is like using CSS where the only selector is `#id` and there are no child selectors. Would you write CSS if you had to do this?

    #username-label { text-align:right }
    #first-name-label { text-align:right }
    ...
    #last-name-label { text-align:right }
    #address-label { text-align:right }
With CSS, you get nice things like:

    #user-form label { text-align: right }
iOS code looks more like the first example but it goes on for hundreds of lines. You can create something like classes by creating real classes, but then you end up with inheritance hell.

I find IB's WYSIWYG to be a lesser evil.




Or you could use a loop :).

    for (UILabel *l in @[self.usernameLabel, self.firstNameLabel, …]) {
        l.textAlignment = UITextAlignmentRight;
        l.textColor = [UIColor redColor];
    }


Or use a helper class (called Design.m/.h) with a call:

  +(void)styleLabelForCertainPage:(UILabel *)label {
       label.textAlignment = UITextAlignmentRight;
       label.textColor = [UIColor redColor];
  }
Then run those through your loops. If you're seriously considering writing a lot of lines for something trivial, then you're probably doing it wrong in Objective-C.


If your iOS code comes out looking that repetitive, sub-optimal code organization is probably the real culprit.


What about UIAppearance? You can specify appearance for a UILabel inside a specific container type.

http://nshipster.com/uiappearance/


I love UIAppearance. I am not too well versed in CSS but UIAppearance adds a lot of what I have seen done with CSS. You can change the default appearance of most default elements really easily, and declare containers inside of that with a whole different set of defaults. It makes styling your app a really painless process. All you have to do to make all the UIToolbars in your app red, for example, is use this one line at the start of your program.

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


You could write generator/setter methods for aligning labels to the right, no need for inheritance here. But, smart categories and inheritance go a long way. The last time I tried IB, I really regretted it because I constantly had to click through all those element layers (object hierarchy inspector is not that good either). After about 2h I gave up and rewrote everything to code-only.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: