ios - 如何在 SWIFT 项目中使用 iosMath?响应。如何将 MTMathUILabel() 添加到项目中?

标签 ios swift math formulas

我是编程新手,但我对如何在 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(),但显然它不是这样工作的。

提前感谢您的帮助。

最佳答案

您发布的代码中的一些问题

  1. 您正在设置一个 IBOutlet,然后实例化另一个具有相同名称的 MTMathUILabel
  2. 你真的不需要调用 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.
    }


}

更好的解决方案如下:

  1. 在 Storyboard中创建一个UIView(因为MTMathUILabel实际上是一个UIView)
  2. 将此 View 的类设置为 MTMathUILabel
  3. 为此 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/

相关文章:

c++ - stringstream sgetn 在 iOS 5.1 上返回 NULL

javascript - WebKit 用户内容 Controller :didReceiveScriptMessage: not getting called for iOS 13

ios - 类型 'UIImageView' 的值在 swift 4 中没有成员 'kf'

php - php中数组的数学运算

android - 如何计算二维点之间的标准偏差

iphone - 不同颜色的UISlider

ios - 如何在 Collection View 中为每个单元格添加 edgeInsets -Swift

iphone - scrollViewDidScroll 委托(delegate)正在自动调用

swift - CLLocationManager 多个委托(delegate)调用 Apple Watch

math - 乘法矩阵