Monospacer 0.6.1

Monospacer 0.6.1

Maintained by Jeff Kelley.



Monospacer

A framework for macOS, iOS, tvOS, and watchOS to create monospaced variants of fonts.

Version Documentation Carthage compatible License Platform

Installation

Swift Package Manager

To use Monospacer with Swift Package Manager, add a dependency to your Package.swift file:

dependencies: [
.package(url: "https://github.com/SlaunchaMan/Monospacer.git", from: "0.6.0")
]

CocoaPods

To use Monospacer with CocoaPods, add a dependency to your Podfile:

target 'MyAwesomeApp' do
  pod 'Monospacer'
end

Then run pod install and use the generated .xcworkspace to open your project.

Carthage

To use Monospacer with Carthage, add a dependency to your Cartfile:

github "SlaunchaMan/Monospacer"

Run carthage update to build the framework. Then follow the rest of the steps in Carthage’s README to add the framework to your project, configure a Run Script build phase, etc.

Manually

To integrate Monospacer manually into your project, drag Monospacer.swift into your Xcode project and add it to all of the targets in which you want to use it.

Using Monospacer

Swift

To use Monospacer, use the extension on UIFont (NSFont for macOS):

let newFont = try? myFont.withMonospaceDigits()

You can also use this on a font descriptor to add the monospaced digits attribute (useful if you need to perform other modifications to the descriptor before using it to create a font):

let newFontDescriptor = myFontDescrpitor.withMonospaceDigits

Objective-C

Monospacer also works with Objective-C:

UIFont *newFont = [myFont fontWithMonospaceDigitsError:NULL];

UIFontDescriptor *newFontDescriptor = myFontDescriptor.descriptorForMonospaceDigits;

Error Handling

Not all fonts support monospaced digits. In the case where a font doesn’t support this feature, Monospacer throws a MonospacerError.fontUnsupported error. On macOS, if font creations fails, Monospacer throws a MonospacerError.fontCreationFailed error. You can handle these in Swift or Objective-C:

// Swift
do {
    let font = try someFont.withMonospaceDigits()
}
catch MonospacerError.fontUnsupported {
    NSLog("Whoops! This font isn't supported!")
}
catch MonospacerError.fontCreationFailed {
    NSLog("Uh-oh. Creating this font failed.")
}
catch {
    fatalError("Unexpected error: \(error.localizedDescription)")
}
// Objective-C
NSError *error = nil;
UIFont *font = [font fontWithMonospaceDigitsError:&error];

if (font == nil) {
    if (error.domain == MonospacerErrorDomain &&
        error.code == MonospacerErrorFontUnsupported) {
        NSLog(@"Whoops! This font isn't supported!");
    }
}

If you don’t care about the errors, you can safely ignore them:

let newFont = try? font.withMonospaceDigits()

myLabel.font = newFont ?? font