Created
January 12, 2016 16:16
-
-
Save keighl/2009a621d0f88a83cf43 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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