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

。_饼干妹妹 提交于 2019-12-10 12:07:43

问题


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.


回答1:


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


}


来源:https://stackoverflow.com/questions/50521273/how-to-use-iosmath-in-swift-project-resp-how-to-add-mtmathuilabel-to-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!