watchkit - 使用 CLKTextProvider.localizableTextProvider(withStringsFileFormatKey:, textProviders:) 组合文本提供程序
问题描述
有没有使用 localizableTextProvider(withStringsFileFormatKey:, textProviders:) 设置复杂功能的官方示例?我可以在生成 SampleTemplate 时填充文本提供程序,但是每当我尝试使用 getTimelineEntries 生成模板时,由 localizableTextProvider 生成的文本提供程序结果总是空白,没有文本。
示例(仅支持 .utilitarianLarge):
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
// Call the handler with the current timeline entry
let template = CLKComplicationTemplateUtilitarianLargeFlat()
template.textProvider = CLKTextProvider.localizableTextProvider(
withStringsFileFormatKey: "testComplication",
textProviders: [
CLKSimpleTextProvider(text: "Hello"),
CLKSimpleTextProvider(text: "World")
]
)
handler(CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template))
}
和 sampleTemplate 作为
func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
// This method will be called once per supported complication, and the results will be cached
switch complication.family {
case .utilitarianLarge:
let template = CLKComplicationTemplateUtilitarianLargeFlat()
template.textProvider = CLKTextProvider.localizableTextProvider(
withStringsFileFormatKey: "testComplication",
textProviders: [
CLKSimpleTextProvider(text: "Hi"),
CLKSimpleTextProvider(text: "World")
]
)
handler(template)
default:
handler(nil)
}
}
与 ckcomplication.strings 作为
"testComplication" = "%@ %@";
示例模板将始终显示文本“Hi World”,而 getCurrentTimelineEntry 的结果将始终显示一个空的复杂功能。
有没有人有幸以这种方式编写文本提供程序?
解决方案
看来这个 API 在 Swift 中被破坏了(从 4.2 开始)。我使用 Objective C 类别解决了这个问题。从这里公然偷走
CLKTextProvider+NNNCompoundTextProviding.h
@interface CLKTextProvider (NNNCompoundTextProviding)
+ (nonnull CLKTextProvider *)nnn_textProviderByJoiningProvider:(nonnull CLKTextProvider *)provider1 andProvider:(nonnull CLKTextProvider *)provider2 withString:(nullable NSString *)joinString;
@end
CLKTextProvider+NNNCompoundTextProviding.m
@implementation CLKTextProvider (NNNCompoundTextProviding)
+ (nonnull CLKTextProvider *)nnn_textProviderByJoiningProvider:(nonnull CLKTextProvider *)provider1 andProvider:(nonnull CLKTextProvider *)provider2 withString:(nullable NSString *)joinString
{
NSString *textProviderToken = @"%@";
NSString *formatString;
if (joinString != nil) {
formatString = [NSString stringWithFormat:@"%@%@%@",
textProviderToken,
joinString,
textProviderToken];
}
else {
formatString = [NSString stringWithFormat:@"%@%@",
textProviderToken,
textProviderToken];
}
return [self textProviderWithFormat:formatString, provider1, provider2];
}
@end
推荐阅读
- python-3.x - 选择元素的文本,但不选择子项的文本
- ruby-on-rails - 重定向到 Ruby on Rails 中的另一个页面
- java - Java while循环中的用户验证测试
- printf - 在 OCaml 中的列表中打印元素
- php - 在codeigniter中批量上传图片
- python - 如何用python下载python?
- apache-poi - 如何使用 POI 4.0.0 绘制 2 线时间序列?
- java - SparkSession 从哪里获取 AWS 凭证?SparkSession 还是 HadoopConfiguration?
- qt - Qt5 什么是 const QModelIndex &sourceParent
- javascript - 工作量大时如何让express Node.JS回复请求?