【问题标题】:How to use iosMath in SWIFT project? Resp. how to add MTMathUILabel() to project?如何在 SWIFT 项目中使用 iosMath?响应。如何将 MTMathUILabel() 添加到项目中?
【发布时间】:2018-05-25 03:51:53
【问题描述】:

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

提前感谢您的帮助。

【问题讨论】:

    标签: ios swift math formulas


    【解决方案1】:

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

    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. 在情节提要中创建一个UIView(因为MTMathUILabel实际上是一个UIView
    2. 将此视图的类设置为MTMathUILabel
    3. 为此视图连接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.
        }
    
    
    }
    

    【讨论】:

    • 感谢您的及时答复。这确实帮助并解决了我的问题。万分感谢:)
    • 很高兴能提供帮助。既然您的问题已经解决,请您接受这个答案吗?
    • 是的,抱歉,我一开始没有注意到接受答案的按钮。再次感谢您的帮助:)
    • @Malik 我得到'NSUnknownKeyException',原因:'[ setValue:forUndefinedKey:]:这个类不符合键标签的键值编码。'每当我尝试这个错误...我该怎么办?我检查了一下,视图只连接到正确的插座......这很奇怪,因为如果我删除连接它可以工作,但如果我重新连接它,它不会,所以我真的不知道我做错了什么...... . 有什么想法吗?
    • @Marybnq 该错误肯定指向在设置之前访问的插座。确保故事板中连接了正确的插座。另外,请确保在调用viewDidLoad 之前未访问出口
    猜你喜欢
    • 2015-10-01
    • 1970-01-01
    • 2018-12-11
    • 2020-09-28
    • 1970-01-01
    • 2012-01-05
    • 2019-04-11
    • 1970-01-01
    • 2012-01-11
    相关资源
    最近更新 更多