WatchKit:我如何实现文本阴影?

时间:2015-05-20 22:55:00

标签: swift text watchkit shadow

在我的Apple Watch应用程序中,我正在尝试将文本放在图像的顶部。我有一个背景图像和标签定位正确的组。但根据图像,我的白色文本有时可能无法读取。深色文本会出现同样的问题,甚至更糟。

有没有办法在我的白色文字下面加上黑色文字并略微偏移?

1 个答案:

答案 0 :(得分:0)

不幸的是,当前版本的WatchKit没有提供“堆叠”或图层WKInterfaceLabel控件的方法。因此,使用第二个标签在白色文本下包含黑色文本的想法是行不通的。

您可以考虑在Drop扩展程序中将您的投影阴影文本渲染为UIImage,并使用带有WKInterfaceImage控件的图像。类似的东西:

- (UIImage *)imageWithText:(NSString *)text shadowBlurRadius:(CGFloat)shadowBlurRadius {

    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowOffset = CGSizeZero;
    shadow.shadowBlurRadius = shadowBlurRadius;
    shadow.shadowColor = [UIColor blackColor];

    NSDictionary *attributes = @{ NSForegroundColorAttributeName    : [UIColor whiteColor],
                                  NSFontAttributeName               : [UIFont boldSystemFontOfSize:36.0f],
                                  NSShadowAttributeName             : shadow };

    CGSize size = [text sizeWithAttributes:attributes];

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width + (2.0f * shadowBlurRadius),
                                                      size.height + (2.0f * shadowBlurRadius)),
                                           NO,
                                           2.0f);

    [text drawAtPoint:CGPointMake(shadowBlurRadius, shadowBlurRadius)
       withAttributes:attributes];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}