无法在多行nsstring上获得投影

时间:2014-09-24 11:42:34

标签: ios nsstring ios8 nsshadow

我试图在不使用弃用API的情况下使用投影来绘制多行文本。它适用于单行。相关代码如下所示:

-(void)drawRect:(CGRect)rect
{
    NSMutableParagraphStyle *paragraph =  [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;

    UIFont *f = [UIFont systemFontOfSize:20.0];
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    [attributes setValuesForKeysWithDictionary:@{ NSFontAttributeName : f,
                                                  NSParagraphStyleAttributeName : paragraph,
                                                  NSForegroundColorAttributeName    : [UIColor blueColor] }];
    NSShadow * shadow = [NSShadow new];
    shadow.shadowOffset = CGSizeMake(4,4);
    shadow.shadowColor = [UIColor redColor];

   [attributes setValue:shadow forKey:NSShadowAttributeName];

    rect.origin.y = 100;
    [@"test string on one line" drawInRect:rect withAttributes:attributes];

    rect.origin.y = 150;
    [@"test string spanning more than one line" drawInRect:rect withAttributes:attributes];
}

,输出如下:

iPhone 6 showing no shadow on multiline text

我在iPhone 5(7.1.2),iPhone 6(8.0)上进行了测试,使用xCode 6构建。我在使用xCode 5进行构建时也在iPhone 5上进行了测试。

1 个答案:

答案 0 :(得分:6)

进行了一些实验,我发现答案是使用NSAttributedString。

虽然这没有显示阴影:

   NSString *s = @"test string spanning more than one line"
   [s drawInRect:rect withAttributes:attributes]

这样做:

   NSAttributedString *as = [[NSAttributedString alloc] initWithString:s attributes:attributes];
   [as drawInRect:rect];

我不会认为这在任何地方都有记录,不愿意听到。