Implements iOS support for AttributedStringOutputFormat
This commit is contained in:
parent
54598263bb
commit
5702cea843
@ -5,8 +5,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
|
|
||||||
import Cocoa
|
import Cocoa
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if os(iOS)
|
||||||
|
import UIKit
|
||||||
|
#endif
|
||||||
|
|
||||||
/// Output format to use to generate an NSAttributedString from the
|
/// Output format to use to generate an NSAttributedString from the
|
||||||
/// highlighted code. A `Theme` is used to determine what fonts and
|
/// highlighted code. A `Theme` is used to determine what fonts and
|
||||||
@ -51,6 +55,7 @@ public extension AttributedStringOutputFormat {
|
|||||||
return NSAttributedString(attributedString: string)
|
return NSAttributedString(attributedString: string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
private mutating func loadFont() -> NSFont {
|
private mutating func loadFont() -> NSFont {
|
||||||
let size = CGFloat(theme.font.size)
|
let size = CGFloat(theme.font.size)
|
||||||
|
|
||||||
@ -65,9 +70,22 @@ public extension AttributedStringOutputFormat {
|
|||||||
return font
|
return font
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if os(iOS)
|
||||||
|
private mutating func loadFont() -> UIFont {
|
||||||
|
|
||||||
|
let size = CGFloat(theme.font.size)
|
||||||
|
let font = UIFont.defaultFont(ofSize: size)
|
||||||
|
return font
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
private extension NSMutableAttributedString {
|
private extension NSMutableAttributedString {
|
||||||
func append(_ string: String, font: NSFont, color: Color) {
|
func append(_ string: String, font: NSFont, color: Color) {
|
||||||
let color = NSColor(
|
let color = NSColor(
|
||||||
@ -110,5 +128,36 @@ private extension NSFont {
|
|||||||
return courier
|
return courier
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if os(iOS)
|
||||||
|
private extension NSMutableAttributedString {
|
||||||
|
func append(_ string: String, font: UIFont, color: Color) {
|
||||||
|
let color = UIColor(
|
||||||
|
red: CGFloat(color.red),
|
||||||
|
green: CGFloat(color.green),
|
||||||
|
blue: CGFloat(color.blue),
|
||||||
|
alpha: CGFloat(color.alpha)
|
||||||
|
)
|
||||||
|
|
||||||
|
let attributedString = NSAttributedString(string: string, attributes: [
|
||||||
|
.foregroundColor: color,
|
||||||
|
.font: font
|
||||||
|
])
|
||||||
|
|
||||||
|
append(attributedString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private extension UIFont {
|
||||||
|
|
||||||
|
static func defaultFont(ofSize size: CGFloat) -> UIFont {
|
||||||
|
guard let menlo = UIFont(name: "Menlo-Regular", size: size) else {
|
||||||
|
return .systemFont(ofSize: size)
|
||||||
|
}
|
||||||
|
|
||||||
|
return menlo
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user