KexAri
KexAri

Reputation: 3977

Applying attributes to text in iOS

I am applying attributes to my text like so:

 //Great the number string and the rect to put it in
    NSString *numberOne = @"1";
    CGRect numberRect = CGRectMake(47, 100, 50, 50);
    //Create the attributes
    UIFont *font = [UIFont fontWithName:@"Academy Engraved LET" size:60];
    NSParagraphStyle *paragraphStyle = [NSParagraphStyle defaultParagraphStyle];
    NSDictionary *numberOneAttributes = @{NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName: font, NSForegroundColorAttributeName: [self darkGoldColor]};
    [numberOne drawInRect:numberRect withAttributes:numberOneAttributes];

I don't understand the array initialisation. Why do we use NSParagraphStyleAttributeName: paragraphStyle, shouldn't the first item be a key? Where are the keys in this dictionary? Any pointers on this would be great. Thanks

Upvotes: 0

Views: 62

Answers (1)

Losiowaty
Losiowaty

Reputation: 8006

You don't initialise any array here. numberOneAttributes is a dictionary. If you format it a little bit can be more readable :

NSDictionary *numberOneAttributes = @{
    NSParagraphStyleAttributeName: paragraphStyle,
    NSFontAttributeName: font, 
    NSForegroundColorAttributeName: [self darkGoldColor]
};

These are key-value pairs. NSParagraphStyleAttributeName is in fact a key - if you CMD+click on it, XCode will take you to its definition which is :

UIKIT_EXTERN NSString * const NSParagraphStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSParagraphStyle, default defaultParagraphStyle

By using system defined keys for attributes, the system can actually understand and apply them to your string.

Upvotes: 1

Related Questions