-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b8c4dd
commit f5b43e7
Showing
3 changed files
with
43 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,35 @@ | ||
import AppKit | ||
import Foundation | ||
|
||
public extension NSMutableAttributedString { | ||
func setTextAttribute(_ key: Key, to newValue: Any, at range: NSRange) { | ||
let range = safeRange(for: range) | ||
let safeRange = safeRange(for: range) | ||
guard length > 0, range.location >= 0 else { return } | ||
beginEditing() | ||
enumerateAttribute(key, in: range, options: .init()) { _, range, _ in | ||
addAttribute(key, value: newValue, range: range) | ||
fixAttributes(in: range) | ||
removeAttribute(key, range: safeRange) | ||
addAttribute(key, value: newValue, range: safeRange) | ||
endEditing() | ||
} | ||
|
||
func setAttributes(_ attrs: [NSAttributedString.Key: Any], range: NSRange) { | ||
let safeRange = self.safeRange(for: range) | ||
guard length > 0, safeRange.location >= 0 else { return } | ||
beginEditing() | ||
attrs.forEach { key, value in | ||
removeAttribute(key, range: safeRange) | ||
addAttribute(key, value: value, range: safeRange) | ||
} | ||
endEditing() | ||
} | ||
|
||
func makeBold(range: NSRange) { | ||
let boldFont = NSFont.boldSystemFont(ofSize: NSFont.systemFontSize) | ||
setTextAttribute(.font, to: boldFont, at: range) | ||
} | ||
|
||
func makeItalic(range: NSRange) { | ||
let italicFont = NSFont.systemFont(ofSize: NSFont.systemFontSize) | ||
italicFont.fontDescriptor.withSymbolicTraits([.italic]) | ||
setTextAttribute(.font, to: italicFont, at: range) | ||
} | ||
} |
This file contains 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
This file contains 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