首页 > 解决方案 > 使用 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 的结果将始终显示一个空的复杂功能。

有没有人有幸以这种方式编写文本提供程序?

标签: watchkitapple-watch-complicationclockkit

解决方案


看来这个 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

推荐阅读