From cc3ef941e14030d2af51638d2ad38642813fbce6 Mon Sep 17 00:00:00 2001 From: Anton Belousov Date: Fri, 3 Feb 2017 03:25:51 +0300 Subject: [PATCH 1/2] Add ability to use code from ObjC --- Pod/Classes/CodeAttributedString.swift | 85 +++++++++++++++----------- Pod/Classes/Theme.swift | 4 +- 2 files changed, 53 insertions(+), 36 deletions(-) diff --git a/Pod/Classes/CodeAttributedString.swift b/Pod/Classes/CodeAttributedString.swift index 5f677b98..9ded96e8 100644 --- a/Pod/Classes/CodeAttributedString.swift +++ b/Pod/Classes/CodeAttributedString.swift @@ -34,13 +34,13 @@ open class CodeAttributedString : NSTextStorage { /// Internal Storage let stringStorage = NSMutableAttributedString(string: "") - + /// Highlightr instace used internally for highlighting. Use this for configuring the theme. open let highlightr = Highlightr()! /// This object will be notified before and after the highlighting. open var highlightDelegate : HighlightDelegate? - + /// Initialize the CodeAttributedString public override init() { @@ -66,7 +66,7 @@ open class CodeAttributedString : NSTextStorage /// Language syntax to use for highlighting. Providing nil will disable highlighting. open var language : String? - { + { didSet { highlight(NSMakeRange(0, stringStorage.length)) @@ -75,7 +75,7 @@ open class CodeAttributedString : NSTextStorage /// Returns a standard String based on the current one. open override var string: String - { + { get { return stringStorage.string @@ -132,7 +132,7 @@ open class CodeAttributedString : NSTextStorage } } } - + func highlight(_ range: NSRange) { if(language == nil) @@ -148,39 +148,39 @@ open class CodeAttributedString : NSTextStorage return; } } - + let string = (self.string as NSString) let line = string.substring(with: range) DispatchQueue.global().async - { - let tmpStrg = self.highlightr.highlight(line, as: self.language!) - DispatchQueue.main.async(execute: { - //Checks to see if this highlighting is still valid. - if((range.location + range.length) > self.stringStorage.length) - { - self.highlightDelegate?.didHighlight?(range, success: false) - return; - } - - if(tmpStrg?.string != self.stringStorage.attributedSubstring(from: range).string) - { - self.highlightDelegate?.didHighlight?(range, success: false) - return; - } - - self.beginEditing() - tmpStrg?.enumerateAttributes(in: NSMakeRange(0, (tmpStrg?.length)!), options: [], using: { (attrs, locRange, stop) in - var fixedRange = NSMakeRange(range.location+locRange.location, locRange.length) - fixedRange.length = (fixedRange.location + fixedRange.length < string.length) ? fixedRange.length : string.length-fixedRange.location - fixedRange.length = (fixedRange.length >= 0) ? fixedRange.length : 0 - self.stringStorage.setAttributes(attrs, range: fixedRange) + { + let tmpStrg = self.highlightr.highlight(line, as: self.language!) + DispatchQueue.main.async(execute: { + //Checks to see if this highlighting is still valid. + if((range.location + range.length) > self.stringStorage.length) + { + self.highlightDelegate?.didHighlight?(range, success: false) + return; + } + + if(tmpStrg?.string != self.stringStorage.attributedSubstring(from: range).string) + { + self.highlightDelegate?.didHighlight?(range, success: false) + return; + } + + self.beginEditing() + tmpStrg?.enumerateAttributes(in: NSMakeRange(0, (tmpStrg?.length)!), options: [], using: { (attrs, locRange, stop) in + var fixedRange = NSMakeRange(range.location+locRange.location, locRange.length) + fixedRange.length = (fixedRange.location + fixedRange.length < string.length) ? fixedRange.length : string.length-fixedRange.location + fixedRange.length = (fixedRange.length >= 0) ? fixedRange.length : 0 + self.stringStorage.setAttributes(attrs, range: fixedRange) + }) + self.endEditing() + self.edited(NSTextStorageEditActions.editedAttributes, range: range, changeInLength: 0) + self.highlightDelegate?.didHighlight?(range, success: true) }) - self.endEditing() - self.edited(NSTextStorageEditActions.editedAttributes, range: range, changeInLength: 0) - self.highlightDelegate?.didHighlight?(range, success: true) - }) - + } } @@ -189,9 +189,24 @@ open class CodeAttributedString : NSTextStorage { highlightr.themeChanged = { _ in - self.highlight(NSMakeRange(0, self.stringStorage.length)) - } + self.highlight(NSMakeRange(0, self.stringStorage.length)) + } } } + +extension CodeAttributedString { + open func setTheme(to theme: String) { + self.highlightr.setTheme(to: theme) + } + open func supportedLanguages() -> [String] { + return highlightr.supportedLanguages() + } + open func availableThemes() -> [String] { + return highlightr.availableThemes() + } + open var theme: Theme { + return highlightr.theme + } +} diff --git a/Pod/Classes/Theme.swift b/Pod/Classes/Theme.swift index e98232e3..4d8f145c 100644 --- a/Pod/Classes/Theme.swift +++ b/Pod/Classes/Theme.swift @@ -27,7 +27,7 @@ private typealias RPThemeDict = [String:[String:AnyObject]] private typealias RPThemeStringDict = [String:[String:String]] /// Theme parser, can be used to configure the theme parameters. -open class Theme { +open class Theme: NSObject { internal let theme : String internal var lightTheme : String! @@ -52,6 +52,7 @@ open class Theme { init(themeString: String) { theme = themeString + super.init() setCodeFont(RPFont(name: "Courier", size: 14)!) strippedTheme = stripTheme(themeString) lightTheme = strippedThemeToString(strippedTheme) @@ -86,6 +87,7 @@ open class Theme { - parameter font: UIFont (iOS or tvOS) or NSFont (OSX) */ + @nonobjc open func setCodeFont(_ font: RPFont) { codeFont = font From e01df5c434cff1f82a983b0bd7ff4ea4e3294f5b Mon Sep 17 00:00:00 2001 From: Anton Belousov Date: Fri, 3 Feb 2017 04:16:39 +0300 Subject: [PATCH 2/2] Force update style --- Pod/Classes/CodeAttributedString.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Pod/Classes/CodeAttributedString.swift b/Pod/Classes/CodeAttributedString.swift index 9ded96e8..de9b4240 100644 --- a/Pod/Classes/CodeAttributedString.swift +++ b/Pod/Classes/CodeAttributedString.swift @@ -209,4 +209,7 @@ extension CodeAttributedString { open var theme: Theme { return highlightr.theme } + open func update() { + self.highlight(NSMakeRange(0, self.stringStorage.length)) + } }