简体   繁体   English

WatchKit:如何实现文本阴影?

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

In my Apple Watch app, I'm trying to place text over the top of an image. 在我的Apple Watch应用中,我试图将文本放在图像上方。 I've got a group with a background image and label positioned correctly. 我有一个背景图像和标签正确放置的小组。 But depending on the image, my white text can sometimes be unreadable. 但是根据图像的不同,我的白色文本有时可能无法读取。 Dark text would have the same problem or be even worse. 深色文字会出现同样的问题,甚至更糟。

Is there a way to have black text just under my white text and slightly offset? 有没有办法让黑色文字恰好位于我的白色文字下方并稍微偏移?

Unfortunately, the current version of WatchKit doesn't provide a way to "stack" or layer WKInterfaceLabel controls. 不幸的是,当前版本的WatchKit并未提供“堆叠”或分层WKInterfaceLabel控件的方法。 So, the idea of including black text under the white text using a second label won't work. 因此,使用第二个标签在白色文本下包含黑色文本的想法将行不通。

You might consider rendering your drop-shadowed text as a UIImage in your Watch extension and using the image with a WKInterfaceImage control. 您可以考虑在Watch扩展程序中将阴影文本呈现为UIImage ,并将该图像与WKInterfaceImage控件一起使用。 Something like: 就像是:

- (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