iosswiftmathformulas

How to use iosMath in SWIFT project? Resp. how to add MTMathUILabel() to project?


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.


Solution

  • A few problems in your posted code

    1. You are setting an IBOutlet then instantiating another MTMathUILabel with the same name
    2. You don't really need to call label.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:

    1. Create a UIView in storyboard (because MTMathUILabel is actually a UIView)
    2. Set this view's class to MTMathUILabel
    3. Connect the IBOutlet for this view

    then 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.
        }
    
    
    }