我是编程新手,但我对如何在iOS上使用iosMath
感兴趣。我已经可以安装Cocoa Pod了,我确实导入了iosMath
项目。问题是:如何可视化数学方程式?
我理解应该使用MTMathUILabel
,但我不知道,如何将其添加到程序中。有没有办法如何创建UIView
或类似的子类,才能够做到?
我的代码示例:
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.
}
}
我尝试将标签连接到我的故事板中的UIView()
和UILabel()
,但显然不是它的工作方式。
提前感谢您的帮助。
答案 0 :(得分:1)
您发布的代码中的一些问题
IBOutlet
,然后使用相同名称实例化另一个MTMathUILabel
label.sizeToFit()
简单的解决方案是删除IBOutlet
,并执行以下操作
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.
}
}
更好的解决方案如下:
UIView
(因为MTMathUILabel
实际上是UIView
)MTMathUILabel
IBOutlet
然后使用以下代码
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.
}
}