Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you extract the text from a CLKRelativeDateTextProvider?

Tags:

swift

clockkit

I'm building up a set of Complications and have come to the CLKComplicationTemplateUtilitarianLargeFlat which only has one textProvider.

I want to display some text, along with a relative date. So I tried doing this:

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

But all I get is:

<CLKRelativeDateTextProvider: 0x79860b80>  

Can you extract the raw text from the CLKRelativeDateTextProvider or combine it with a CLKSimpleTextProvider in some way?

like image 295
svarrall Avatar asked Jun 25 '15 14:06

svarrall


1 Answers

Pass in the CLKRelativeDateTextProvider object to the format string, as mentioned in Apple's code:

@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

Here is an example:

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

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

The time shown in the date provider will still update as time passes without having to refresh anything.

like image 95
Dustin Avatar answered Nov 18 '22 17:11

Dustin