Swiftpack.co - Package - SlaunchaMan/Monospacer
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

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

Github

link
Stars: 0

Releases

0.3.1 - 2018-07-12T05:12:39

  • Adds documentation generated by jazzy

0.3.0 - 2018-07-12T04:50:28

  • Adds MonospacerError enum and begins throwing MonospacerError.fontUnsupported errors for unsupported fonts
  • macOS: Throw a MonospacerError.fontCreationFailed error instead of returning nil fonts
  • Changes watchOS example interface controller to Objective-C to demonstrate Objective-C error handling

0.2.0 - 2018-07-12T03:45:00

  • Adds tests for macOS, iOS, and tvOS
  • Adds hasMonospacedFontSelector property to UIFontDescriptor and NSFontDescriptor
  • Sets Swift version to 4.0

0.1.0 - 2018-07-11T21:05:29

  • Adds README.md and CHANGELOG.md
  • Adds documentation for font extensions