2015-12-09 17 views
0

文字我申请属性我的文字像这样:应用属性中的iOS

//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]; 

我不明白数组的初始化。为什么我们使用NSParagraphStyleAttributeName: paragraphStyle,不应该把第一项作为关键?这本词典里的钥匙在哪里?任何指针都会很棒。谢谢

+0

http://clang.llvm.org/docs/ObjectiveCLiterals.html – Larme

回答

1

你不在这里初始化任何数组。 numberOneAttributes是一个词典。如果将其格式化一点点,则可读性更高:

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

这些是键值对。 NSParagraphStyleAttributeName事实上是一个关键 - 如果你CMD +点击它时,Xcode会带你到它的定义是:

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

通过使用系统定义的键的属性,该系统实际上可以理解,并将它们应用到你串。

+0

非常感谢!现在明白了 – KexAri