Forked from Tricertops/UIFont+Features.swift
Last active
September 1, 2021 16:39
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import CoreText | |
extension UIFont { | |
typealias Feature = (type: Int, selector: Int) | |
struct Features { | |
static var ProportionalNumbers: Feature = (kNumberSpacingType, kProportionalNumbersSelector) | |
static var AlternatePunctuation: Feature = (kCharacterAlternativesType, 1) // Magic! | |
// Add more... | |
} | |
func fontByAddingFeatures(features: [Feature]) -> UIFont { | |
// Using Objective-C classes because of some compiler errors... | |
let attributes = self.fontDescriptor().fontAttributes() | |
var featureDisctionaries: Array<NSObject> = []; | |
let attr: AnyObject! = attributes[UIFontDescriptorFeatureSettingsAttribute] | |
if let existingFeatureDisctionaries = attr as? Array<NSObject> { | |
featureDisctionaries = existingFeatureDisctionaries; | |
} | |
for feature in features { | |
featureDisctionaries.append([ | |
UIFontFeatureTypeIdentifierKey: feature.type, | |
UIFontFeatureSelectorIdentifierKey: feature.selector, | |
]) | |
} | |
let descriptor = self.fontDescriptor().fontDescriptorByAddingAttributes([ | |
UIFontDescriptorFeatureSettingsAttribute: featureDisctionaries, | |
]) | |
return UIFont(descriptor: descriptor, size: 0) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
label.font.fontByAddingFeatures([ | |
UIFont.Features.ProportionalNumbers, | |
UIFont.Features.AlternatePunctuation, | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment