7

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?

svarrall
  • 8,545
  • 2
  • 27
  • 32

2 Answers2

3

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.

Dustin
  • 1,030
  • 11
  • 15
-2

With the current version of ClockKit you can not pull any String data from a text provider nor combine 2 or more text providers. The only text providers available to you are:

  • CLKDateTextProvider
  • CLKRelativeDateTextProvider
  • CLKSimpleTextProvider
  • CLKTimeIntervalTextProvider
  • CLKTimeTextProvider

To answer your question you will not be able to display the word "Next" and then a date in your complication. Due to the way ClockKit is designed and the way your complication data should be designed, however, you shouldn't be needing to display "Next" at all. Your complication should be automatically showing the next item from your data relative to the current time.

  • 1
    It appears this is true for Swift projects, although CLKTextProvider textProviderWithFormat is available if using Obj-C – svarrall Nov 17 '15 at 17:18