问题
I'm trying to use iosMath but it shows nothing. How can I use the iosMath library to draw math labels?
https://github.com/kostub/iosMath
iosMath
is a library for displaying beautifully rendered math equations in iOS and MacOS applications. It typesets formulae written using the LaTeX in aUILabel
equivalent class. It uses the same typesetting rules as LaTeX and so the equations are rendered exactly as LaTeX would render them.
I installed pod file.
import UIKit
import Foundation
import CoreGraphics
import QuartzCore
import CoreText
import iosMath
class Deneme_VC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let label: MTMathUILabel = MTMathUILabel()
label.latex = "x = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}"
//ADD THIS LABE TO THE VIEW HEIRARCHY
view.addSubview(label)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
enter image description here
回答1:
I assume this is somewhere in that library's docs, but...
You need to either give the label
an explicit frame, or call sizeToFit()
:
import UIKit
import CoreGraphics
import QuartzCore
import CoreText
import iosMath
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label: MTMathUILabel = MTMathUILabel()
label.latex = "x = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}"
//ADD THIS LABE TO THE VIEW HEIRARCHY
view.addSubview(label)
// need to call sizeToFit() to get it to calculate the frame
label.sizeToFit()
// center it in the view
label.center = view.center
// so we can see the frame
label.backgroundColor = .yellow
}
}
Result:
来源:https://stackoverflow.com/questions/63638064/drawing-labels-using-iosmath-library