Search code examples
iosobjective-cswiftwatchkitxcode7

WatchKit SF monospaced number


Is there any way to set monospaced for San Francisco font in WatchKit? In Introducing the New System Fonts, the presenter only shows for AppKit.

Sample code is appreciated.

Xcode: 7.0 beta WatchOS: 2.0


Solution

  • Sample code:

    UIFont *systemFont = [UIFont systemFontOfSize:20];  
    UIFontDescriptor *monospacedNumberFontDescriptor = [systemFont.fontDescriptor fontDescriptorByAddingAttributes: @{  
         UIFontDescriptorFeatureSettingsAttribute: @[@{  
              UIFontFeatureTypeIdentifierKey: @6,  
              UIFontFeatureSelectorIdentifierKey: @0  
         }]  
    }];  
    
    UIFont *monospacedNumberSystemFont = [UIFont fontWithDescriptor:monospacedNumberFontDescriptor size:0];  
    NSMutableAttributedString *monospacedString = [[NSMutableAttributedString alloc] initWithString:@"1234567890"];  
    [monospacedString addAttribute:NSFontAttributeName value:monospacedNumberSystemFont range:NSMakeRange(0, monospacedString.string.length)];  
    
    [label setAttributedText:monospacedString];