Skip to content

Instantly share code, notes, and snippets.

@jhoughjr
Last active March 2, 2022 10:45
Show Gist options
  • Save jhoughjr/407d89033c853e87d2bd1c6f2f260b90 to your computer and use it in GitHub Desktop.
Save jhoughjr/407d89033c853e87d2bd1c6f2f260b90 to your computer and use it in GitHub Desktop.
func toggleBoldOverRange(range:NSRange) {
if let string = textView?.textStorage?.attributedSubstringFromRange(range) {
let attribs = string.fontAttributesInRange(NSRange(location: 0, length: string.length))
if let font = attribs["NSFont"] as? NSFont {
if font.isBold() {
if textView!.shouldChangeTextInRange(range, replacementString: string.string) {
textView?.textStorage?.beginEditing()
textView?.textStorage?.applyFontTraits(.UnboldFontMask, range: range)
textView?.textStorage?.endEditing()
}
}else {
if textView!.shouldChangeTextInRange(range, replacementString: string.string) {
textView?.textStorage?.beginEditing()
textView?.textStorage?.applyFontTraits(.BoldFontMask, range: range)
textView?.textStorage?.endEditing()
}
}
}
}
}
ffunc toggleTaskListOverRange(range:NSRange) {
if let lines = textView?.textStorage?.attributedSubstringFromRange(range).mutableLines() where !lines.isEmpty {
let replacementString = NSMutableAttributedString()
lines.forEach { (attributedString) in
if attributedString.hasCheckBox() {
attributedString.deleteCharactersInRange(NSRange(location: 0, length: 2))
// let newLine = NSAttributedString(string: "\n")
// attributedString.appendAttributedString(newLine)
replacementString.appendAttributedString(attributedString)
}else {
if !attributedString.string.isEmpty {
var mutableAttribs = attributedString.attributesAtIndex(0, effectiveRange: nil)
if let currentFont = mutableAttribs[NSFontAttributeName] as? NSFont {
let size = currentFont.pointSize
mutableAttribs[NSFontAttributeName] = NSFont(name: "Hellvetica", size: size)
let box = NSAttributedString.taskUncheckedStringWith(mutableAttribs)
attributedString.insertAttributedString(box, atIndex: 0)
// let newLine = NSAttributedString(string: "\n")
// attributedString.appendAttributedString(newLine)
replacementString.appendAttributedString(attributedString)
}
}
}
}
if ((textView?.shouldChangeTextInRange(range, replacementString: replacementString.string)) != nil) {
textView?.textStorage?.beginEditing()
textView?.textStorage?.replaceCharactersInRange(range, withAttributedString: replacementString)
textView?.textStorage?.endEditing()
textView?.didChangeText()
}
}
}
extension NSFont {
func isBold() -> Bool {
return NSFontManager.sharedFontManager().traitsOfFont(self).contains(.BoldFontMask)
}
func isItalic() -> Bool {
return NSFontManager.sharedFontManager().traitsOfFont(self).contains(.ItalicFontMask)
}
func boldFont() -> NSFont {
return NSFontManager.sharedFontManager().convertFont(self, toHaveTrait: .BoldFontMask)
}
func italicFont() -> NSFont {
return NSFontManager.sharedFontManager().convertFont(self, toHaveTrait: .ItalicFontMask)
}
func nonBoldFont() -> NSFont {
return NSFontManager.sharedFontManager().convertFont(self, toHaveTrait: .UnboldFontMask)
}
func nonItalicFont() -> NSFont {
return NSFontManager.sharedFontManager().convertFont(self, toHaveTrait: .UnitalicFontMask)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment