I am new to programming, but I am interested in how to use iosMath
for iOS. I could instal Cocoa Pod already and I did import iosMath
to project. Question is: how to visualise math equations?
I understand that it should be used MTMathUILabel
for that, however I do not know, how to add it to program. Is there a way how to create subclass of UIView
or something, to be able able to do it?
Here sample of my code:
import UIKit
import Foundation
import CoreGraphics
import QuartzCore
import CoreText
import iosMath
class ViewController: UIViewController {
@IBOutlet weak var label: MTMathUILabel!
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}"
label.sizeToFit()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I tried to connect label to UIView()
and UILabel()
in my Storyboard, but obviously thats not how it works.
Thank you in advance for any help.
A few problems in your posted code
IBOutlet
then instantiating another MTMathUILabel
with the same namelabel.sizeToFit()
Simple solution is to remove the IBOutlet
, and do as follows
class ViewController: 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.
}
}
Better solution is as follows:
UIView
in storyboard (because MTMathUILabel
is actually a UIView
)MTMathUILabel
IBOutlet
for this viewthen use the following code
class ViewController: UIViewController {
@IBOutlet weak var label: MTMathUILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//NO NEED TO INSTANTIATE A NEW INSTANCE HERE
label.latex = "x = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}"
//NO NEED TO CALL sizeToFit()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}