-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathMathLabel.swift
More file actions
95 lines (83 loc) · 2.62 KB
/
MathLabel.swift
File metadata and controls
95 lines (83 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// MathLabel.swift
// SwiftMathExample
//
// SwiftUI wrapper for MTMathUILabel, cross-platform (iOS + macOS).
//
// This software may be modified and distributed under the terms of the
// MIT license. See the LICENSE file for details.
//
import SwiftUI
import iosMath
/// A SwiftUI view that renders a LaTeX math formula using MTMathUILabel.
struct MathLabel: View {
let latex: String
var fontSize: CGFloat = 15
var mode: MTMathUILabelMode = .display
var alignment: MTTextAlignment = .left
/// Applies a light yellow background, mirroring the ObjC test example highlights.
var highlighted: Bool = false
var leftInset: CGFloat = 0
var rightInset: CGFloat = 0
var body: some View {
_MathLabelRepresentable(
latex: latex,
fontSize: fontSize,
mode: mode,
alignment: alignment,
highlighted: highlighted,
leftInset: leftInset,
rightInset: rightInset
)
}
}
// MARK: - Platform representables
#if os(iOS)
import UIKit
private struct _MathLabelRepresentable: UIViewRepresentable {
let latex: String
let fontSize: CGFloat
let mode: MTMathUILabelMode
let alignment: MTTextAlignment
let highlighted: Bool
let leftInset: CGFloat
let rightInset: CGFloat
func makeUIView(context: Context) -> MTMathUILabel {
MTMathUILabel()
}
func updateUIView(_ label: MTMathUILabel, context: Context) {
label.latex = latex
label.fontSize = fontSize
label.mode = mode
label.textAlignment = alignment
label.contentInsets = UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)
label.backgroundColor = highlighted
? UIColor(hue: 0.15, saturation: 0.2, brightness: 1.0, alpha: 1.0)
: .clear
}
}
#elseif os(macOS)
import AppKit
private struct _MathLabelRepresentable: NSViewRepresentable {
let latex: String
let fontSize: CGFloat
let mode: MTMathUILabelMode
let alignment: MTTextAlignment
let highlighted: Bool
let leftInset: CGFloat
let rightInset: CGFloat
func makeNSView(context: Context) -> MTMathUILabel {
MTMathUILabel()
}
func updateNSView(_ label: MTMathUILabel, context: Context) {
label.latex = latex
label.fontSize = fontSize
label.mode = mode
label.textAlignment = alignment
label.contentInsets = NSEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)
label.backgroundColor = highlighted
? NSColor(hue: 0.15, saturation: 0.2, brightness: 1.0, alpha: 1.0)
: .clear
}
}
#endif