Skip to content

Commit 1f47ac8

Browse files
committed
Allow dynamic change of default font.
1 parent f9ff2d3 commit 1f47ac8

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

Aztec/Classes/TextKit/TextView.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,26 @@ open class TextView: UITextView {
226226

227227
// MARK: - Properties: UI Defaults
228228

229-
public let defaultFont: UIFont
229+
public var monospaceFont: UIFont = UIFont(descriptor:UIFontDescriptor(name: "Courier", size: 12), size:12)
230+
231+
public var defaultFont: UIFont {
232+
didSet {
233+
refreshFont(oldFont: oldValue, newFont: defaultFont)
234+
}
235+
}
236+
237+
private func refreshFont(oldFont: UIFont, newFont: UIFont) {
238+
let fullRange = NSRange(location: 0, length: textStorage.length)
239+
240+
textStorage.beginEditing()
241+
textStorage.enumerateAttributes(in: fullRange, options: []) { (attributes, subrange, stop) in
242+
if let currentFont = attributes[.font] as? UIFont, currentFont == oldFont {
243+
textStorage.addAttribute(.font, value: newFont, range: subrange)
244+
}
245+
}
246+
textStorage.endEditing()
247+
}
248+
230249
public let defaultParagraphStyle: ParagraphStyle
231250
var defaultMissingImage: UIImage
232251

Example/Example/EditorDemoController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,15 @@ class EditorDemoController: UIViewController {
195195
} else {
196196
html = ""
197197
}
198-
198+
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(changeFont))
199199
editorView.setHTML(html)
200200
editorView.becomeFirstResponder()
201201
}
202202

203+
@objc func changeFont() {
204+
editorView.richTextView.defaultFont = UIFont.preferredFont(forTextStyle: .callout)
205+
}
206+
203207
override func viewWillAppear(_ animated: Bool) {
204208
super.viewWillAppear(animated)
205209

0 commit comments

Comments
 (0)