On iOS, I load a custom font in my project by adding its file name (an .otf file) to the info.plist file and then using this line of code:
UIFont myF
Extension for UIFont
in Swift:
extension UIFont {
func smallCaps() -> UIFont {
let settings = [[UIFontFeatureTypeIdentifierKey: kLowerCaseType, UIFontFeatureSelectorIdentifierKey: kLowerCaseSmallCapsSelector]]
let attributes: [String: AnyObject] = [UIFontDescriptorFeatureSettingsAttribute: settings, UIFontDescriptorNameAttribute: fontName]
return UIFont(descriptor: UIFontDescriptor(fontAttributes: attributes), size: pointSize)
}
}
Usage:
label.font = UIFont(name: "SourceSansPro-Regular", size: 12)?.smallCaps()
Example:
macOS version:
extension NSFont {
func smallCaps() -> NSFont? {
let settings = [[NSFontFeatureTypeIdentifierKey: kLowerCaseType, NSFontFeatureSelectorIdentifierKey: kLowerCaseSmallCapsSelector]]
let attributes: [String: AnyObject] = [NSFontFeatureSettingsAttribute: settings as AnyObject, NSFontNameAttribute: fontName as AnyObject]
return NSFont(descriptor: NSFontDescriptor(fontAttributes: attributes), size: pointSize)
}
}
IMPORTANT:
not every font works with it, It's dependent on whether those characters are included in the font or not as @Anthony Mattox said.
remember to set the string as: Example
, not EXAMPLE
.
Array<NDSictionary>
, not NSDictonary
.Small caps are enabled in the font through an open type feature. In iOS 7 we can use a font descriptor to access open type features and enable small caps.
This question goes into how to turn on small caps using core text, but the same can be done for UIFonts and UIKit views just as easily. You'll need to create a UIFontDescriptor
and set the UIFontDescriptorFeatureSettingsAttribute
to an array of dictionaries for the features you want to enable.
Each font feature dictionary contains a key and value to specify the feature type, and a key and value for the feature selector. Depending on the font you're using, you'll need to find the correct values corresponding to small caps. You can find these in the array that the commented section logs.
This category will generate a UIFont object with small caps enabled. You'll need to add the correct font name.
#import "UIFont+SmallCaps.h"
#import <CoreText/CoreText.h>
@implementation UIFont (SmallCaps)
+ (UIFont *) applicationSmallCapsFontWithSize:(CGFloat) size {
/*
// Use this to log all of the properties for a particular font
UIFont *font = [UIFont fontWithName: fontName size: fontSize];
CFArrayRef fontProperties = CTFontCopyFeatures ( ( __bridge CTFontRef ) font ) ;
NSLog(@"properties = %@", fontProperties);
*/
NSArray *fontFeatureSettings = @[ @{ UIFontFeatureTypeIdentifierKey: @(kLowerCaseType),
UIFontFeatureSelectorIdentifierKey : @(kLowerCaseSmallCapsSelector) } ];
NSDictionary *fontAttributes = @{ UIFontDescriptorFeatureSettingsAttribute: fontFeatureSettings ,
UIFontDescriptorNameAttribute: FONT_NAME } ;
UIFontDescriptor *fontDescriptor = [ [UIFontDescriptor alloc] initWithFontAttributes: fontAttributes ];
return [UIFont fontWithDescriptor:fontDescriptor size:size];
}
@end