Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Getting around Swift 2’s lack of compatibility with vararg methods for complication text providers.
@interface CLKTextProvider (NNNCompoundTextProviding)
+ (nonnull CLKTextProvider *)nnn_textProviderByJoiningProvider:(nonnull CLKTextProvider *)provider1 andProvider:(nonnull CLKTextProvider *)provider2 withString:(nullable NSString *)joinString;
@end
@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
@emma-k-alexandra

This comment has been minimized.

Copy link

@emma-k-alexandra emma-k-alexandra commented Mar 19, 2019

4 years later, this still appears to be the only way to have compound text providers within a swift project.

@gongzhang

This comment has been minimized.

Copy link

@gongzhang gongzhang commented Apr 6, 2020

5 years later, this still appears to be the only way to have compound text providers within a swift project.

@SlaunchaMan thanks for saving me 1 hour.

UPDATE: As noted in the comments below, on watchOS 6 it is possible to use init(format:_:). 👍

@UInt2048

This comment has been minimized.

Copy link

@UInt2048 UInt2048 commented Dec 3, 2020

It took them until watchOS 6 to add init(format:_:) to CLKTextProvider, so a correction to the above comments is: For watchOS 5 and prior, this is the only good way to do it...

@gongzhang

This comment has been minimized.

Copy link

@gongzhang gongzhang commented Mar 27, 2021

init(format:_:) can be used to create multi-color text. (Top left corner)

@SwiftiSwift

This comment has been minimized.

Copy link

@SwiftiSwift SwiftiSwift commented May 13, 2021

Can someone post an example how to use the init(format:_:)? @gongzhang @UInt2048

@gongzhang

This comment has been minimized.

Copy link

@gongzhang gongzhang commented May 13, 2021

let text1 = CLKSimpleTextProvider(text: "red")
text1.tintColor = .red

let text2= CLKSimpleTextProvider(text: "green")
text2.tintColor = .green

// secret sauce:
// just pass CLKTextProvider objects as arguments
let finalText = CLKTextProvider(format: "%@ and %@", text1, text2)
@SwiftiSwift

This comment has been minimized.

Copy link

@SwiftiSwift SwiftiSwift commented May 13, 2021

Thank you very much! Just wanted to add how to use it with CLKRelativeDateTextProvider:

let myComplicationTemplate = CLKComplicationTemplateUtilitarianLargeFlat()
let dateProvider = CLKRelativeDateTextProvider()

dateProvider.date = Date()
dateProvider.calendarUnits = [.hour, .minute]
dateProvider.relativeDateStyle = .naturalAbbreviated

let finalText = CLKTextProvider(format: "GBT * %@", dateProvider)

myComplicationTemplate.textProvider = finalText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment