swiftnsfont

Equivalent of UIFont .withSize in NSFont


I am working on getting the HandyUIKit String extensions working with AppKit.

What is the equivalent for

var font : UIFont    
font.withSize(font.pointSize * scriptedTextSizeRatio)

in Appkit and NSFont?

var font : NSFont    
font.withSize(font.pointSize * scriptedTextSizeRatio)

Here is the code section I am working on:

 let capturedSubstring = unprocessedString.attributedSubstring(from: match.range(at: 1)).mutableCopy() as! NSMutableAttributedString
 let captureFullRange = NSRange(location: 0, length: capturedSubstring.length)
 capturedSubstring.addAttribute(.font, value: font.withSize(font.pointSize * scriptedTextSizeRatio), range: captureFullRange)

Solution

  • NSFont doesn't seem to have an equivalent method withSize like UIFont.

    One solution would be to use:

    var font: NSFont = ... // some font
    var newFont = NSFont(descriptor: font.fontDescriptor, size: font.pointSize * scriptedTextSizeRatio)
    

    Note that newFont will be optional so check the result as needed.