swift - 你能从 CLKRelativeDateTextProvider 中提取文本吗?

标签 swift clockkit

我正在构建一组复杂功能,并来到了只有一个文本提供程序的 CLKComplicationTemplateUtilitarianLargeFlat。

我想显示一些文本以及相关日期。所以我尝试这样做:

let date = CLKRelativeDateTextProvider(date: NSDate(), style: style, units: units)  
let template = CLKComplicationTemplateUtilitarianLargeFlat()  
template.textProvider = CLKSimpleTextProvider(text: "next: \(date)")  

但我得到的只是:

<CLKRelativeDateTextProvider: 0x79860b80>  

您能否从 CLKRelativeDateTextProvider 中提取原始文本或以某种方式将其与 CLKSimpleTextProvider 组合?

最佳答案

将 CLKRelativeDateTextProvider 对象传入格式字符串,如 Apple 代码中所述:

@interface CLKTextProvider : NSObject <NSCopying>

// By passing one or more CLKTextProviders in the format substitutions, you can add text around the output of a text provider.
+ (CLKTextProvider *)textProviderWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1, 2);

@property (nonatomic) UIColor *tintColor;

@end

这是一个例子:

id relativeDate = [CLKRelativeDateTextProvider textProviderWithDate:[NSDate dateWithTimeIntervalSinceNow:12 * 60]
                                                              style:CLKRelativeDateStyleNatural
                                                              units:NSCalendarUnitMinute];

template.textProvider = [CLKTextProvider textProviderWithFormat:@"next: %@", relativeDate];

日期提供程序中显示的时间仍会随着时间的推移而更新,而无需刷新任何内容。

关于swift - 你能从 CLKRelativeDateTextProvider 中提取文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31053447/

相关文章:

ios - 允许使用 WKWebView 自签名证书

ios - 容器 View 中的数据仅传递给子 VC 一次

apple-watch - Apple Watch 的复杂功能数据更新流程是怎样的?

uikit - CLKComplication tintColor 不起作用

swift - MFMailComposeViewController 发送和取消都不起作用

ios - 如何在 UIView 中插入图片?

watchkit - reloadTimeline() 不更新复杂性

ios - SpriteKit zPosition 背景

ios - ClockKit CLKComplicationDataSource 缺少向后事件