如何在SWIFT项目中使用iosMath?分别如何将MTMathUILabel()添加到项目? [英] How to use iosMath in SWIFT project? Resp. how to add MTMathUILabel() to project?
问题描述
我是编程新手,但是我对如何在iOS中使用iosMath
感兴趣.我已经可以安装Cocoa Pod了,并且确实导入了iosMath
到项目中.问题是:如何可视化数学方程式?
我知道应该为此使用MTMathUILabel
,但是我不知道如何将其添加到程序中.有没有办法创建UIView
的子类或某种东西,以便能够做到呢?
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?
这里是我的代码示例:
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()
,但是显然那不是它的工作原理.
I tried to connect label to UIView()
and UILabel()
in my Storyboard, but obviously thats not how it works.
在此先感谢您的帮助.
推荐答案
已发布代码中的一些问题
A few problems in your posted code
- 您要设置一个
IBOutlet
,然后实例化另一个具有相同名称的MTMathUILabel
- 您真的不需要致电
label.sizeToFit()
- You are setting an
IBOutlet
then instantiating anotherMTMathUILabel
with the same name - You don't really need to call
label.sizeToFit()
简单的解决方法是删除IBOutlet
,并执行以下操作
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.
}
}
更好的解决方案如下:
- 在情节提要中创建一个
UIView
(因为MTMathUILabel
实际上是一个UIView
) - 将此视图的类设置为
MTMathUILabel
- 为此视图连接
IBOutlet
- Create a
UIView
in storyboard (becauseMTMathUILabel
is actually aUIView
) - Set this view's class to
MTMathUILabel
- Connect the
IBOutlet
for this view
然后使用以下代码
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.
}
}
这篇关于如何在SWIFT项目中使用iosMath?分别如何将MTMathUILabel()添加到项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!