Skip to content

Instantly share code, notes, and snippets.

@keighl
Created January 12, 2016 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keighl/2009a621d0f88a83cf43 to your computer and use it in GitHub Desktop.
Save keighl/2009a621d0f88a83cf43 to your computer and use it in GitHub Desktop.
class Document: NSDocument {
@IBAction func fontSizeChanged(sender: NSControl) {
var newSize = CGFloat(sender.floatValue)
if var font = textView?.typingAttributes[NSFontAttributeName] as? NSFont {
font = NSFontManager.sharedFontManager().convertFont(font, toSize: newSize)
applyNewAttributes([NSFontAttributeName: font])
}
}
func applyNewAttributes(attributes: [String: AnyObject]) {
if let changeRanges = textView?.rangesForUserCharacterAttributeChange {
textView?.shouldChangeTextInRanges(changeRanges, replacementStrings: nil)
textView?.textStorage?.beginEditing()
for (_, value) in changeRanges.enumerate() {
textView?.textStorage?.addAttributes(attributes, range: value.rangeValue)
}
textView?.textStorage?.endEditing()
textView?.didChangeText()
}
if var typingAttributes = textView?.typingAttributes {
typingAttributes.updateWithDictionary(attributes)
textView?.typingAttributes = typingAttributes
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment