Bryan Schmiedeler
Bryan Schmiedeler

Reputation: 3137

Anything like "styles" in iOS

A bit new to iOS but learning fast.

I have a number of labels and UIText fields in my app. After looking at it on a real iPad I decided I wanted to change the text on a series of fields from whatever its was to Helv Neu condensed Bold size 24. I am thinking there must be a better way, some type of style I can set x number of fields to and then just set the style, but I cannot find anything like that.

Help!

Upvotes: 0

Views: 192

Answers (3)

Tobias Löfstrand
Tobias Löfstrand

Reputation: 66

I realize this is an oldish question, but here are some other suggestions of frameworks that allow you to use reusable styles in your application:

Open source:

  • InterfaCSS - https://github.com/tolo/InterfaCSS
    • Uses advanced CSS-based stylesheets with UIKit property names and support for nested declarations, constants etc
    • iOS only

Closed source (but free):

  • nativeCSS - http://nativecss.com/
    • Uses advanced stylesheets with standard CSS/HTML property names, with some non-standard additions
    • Support for multiple platforms

Upvotes: 0

Aaron Brager
Aaron Brager

Reputation: 66302

DB5

One option is to use DB5, the system invented by Vesper, to store fonts, colors, etc. in a plist. Then when you want to change them, you can change them in one place, forever. You could even have your app download a new Plist from a server, allowing you to modify your app's appearance remotely.

Attributed String dictionaries

Another option is to use a constants file to define Attributed String dictionaries.

Basic Example

+ (NSDictionary *) biggerLetterSpacingText
{
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [dictionary setObject:[NSNumber numberWithFloat:1.3] forKey:NSKernAttributeName];
    return dictionary;
}

More-than-basic Example

+ (NSDictionary *) linkAttributes
{
    static NSMutableDictionary *dictionary;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dictionary = [NSMutableDictionary dictionary];

        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

        [dictionary setObject:paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
    });

    UIColor *tintColor = [[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] window] tintColor];

    UIColor *colorToUse;

    if (CGColorEqualToColor(tintColor.CGColor, [UIColor blueColor].CGColor)) {
        // links are enabled
        colorToUse = [UIColor blueColor];
    } else {
        // grey them out
        colorToUse = tintColor;
    }

    [dictionary setObject:colorToUse forKey:(NSString *)kCTForegroundColorAttributeName];
    return dictionary;
}

Notes

This particular code could be used for coloring hyperlinks on iOS 7. Feel free to use something simpler if that helps.

The dispatch_once code at the top makes sure that one dictionary is used all the time (so you don't end up allocating a new one every time you call this method.

The code towards the bottom adjusts the tint color based on the current app's tint color. So if a UIAlertView or UIActionSheet pops up, all of the links can turn from blue to gray.

Example Code

To use it (on a label, for example):

NSString *text = @"Hello, world!";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString addAttributes:[MyConstants linkAttributes] range:NSMakeRange(0, [text length])];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.attributedText = attributedString;

Upvotes: 3

JeroValli
JeroValli

Reputation: 569

You should use a category. Here is an example:

@interface UILabel (FontName)

- (void)setFontName:(NSString *)name;

@end


@implementation UILabel (FontName)

- (void)setFontName:(NSString *)name
{

    self.font = [UIFont fontWithName:name size:self.font.pointSize];
}

@end

Upvotes: 0

Related Questions