在iOS中将属性应用于文本

时间:2015-12-09 08:26:23

标签: ios nsstring

我正在将属性应用于我的文本:

 //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,第一项不应该是关键?这本词典中的键在哪里?对此的任何指示都会很棒。感谢

1 个答案:

答案 0 :(得分: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

通过使用系统定义的属性键,系统可以实际理解并将它们应用于您的字符串。

相关问题