繁体   English   中英

WatchKit:如何实现文本阴影?

[英]WatchKit: How can I implement a text shadow?

在我的Apple Watch应用中,我试图将文本放在图像上方。 我有一个背景图像和标签正确放置的小组。 但是根据图像的不同,我的白色文本有时可能无法读取。 深色文字会出现同样的问题,甚至更糟。

有没有办法让黑色文字恰好位于我的白色文字下方并稍微偏移?

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

您可以考虑在Watch扩展程序中将阴影文本呈现为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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM