簡體   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