我是编程新手,但我对如何在 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.
}
}
我试图在我的 Storyboard 中将标签连接到 UIView()
和 UILabel()
,但显然它不是这样工作的。
提前感谢您的帮助。
最佳答案
您发布的代码中的一些问题
- 您正在设置一个
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.
}
}
更好的解决方案如下:
- 在 Storyboard中创建一个
UIView
(因为MTMathUILabel
实际上是一个UIView
) - 将此 View 的类设置为
MTMathUILabel
- 为此 View 连接
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.
}
}
关于ios - 如何在 SWIFT 项目中使用 iosMath?响应。如何将 MTMathUILabel() 添加到项目中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50521273/