【问题标题】:WatchKit: How can I implement a text shadow?WatchKit:如何实现文本阴影?
【发布时间】:2015-08-02 09:54:57
【问题描述】:

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

有没有办法在我的白色文本下方添加黑色文本并稍微偏移?

【问题讨论】:

  • 不知道如何在 watchkit 中为文本添加阴影,但通过将标签放在组中然后分配背景图像(即渐变)来分组,我取得了很好的效果.

标签: swift text watchkit shadow


【解决方案1】:

不幸的是,当前版本的 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;
}

【讨论】:

  • NSShadow 在 Apple Watch 上不可用
  • 您是正确的,NSShadow 在 Watch 上不可用,但在 iPhone 上可用。此代码在 iPhone 上运行在 v1 的 Watch 扩展中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 2016-01-14
  • 2012-08-21
相关资源
最近更新 更多