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

前端 未结 1 1081
囚心锁ツ
囚心锁ツ 2021-01-17 04:11

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

1条回答
  •  一整个雨季
    2021-01-17 04:46

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

    0 讨论(0)
提交回复
热议问题