如何在SWIFT项目中使用iosMath?分别如何将MTMathUILabel()添加到项目? [英] How to use iosMath in SWIFT project? Resp. how to add MTMathUILabel() to project?

查看:221
本文介绍了如何在SWIFT项目中使用iosMath?分别如何将MTMathUILabel()添加到项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,但是我对如何在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

  1. 您要设置一个IBOutlet,然后实例化另一个具有相同名称的MTMathUILabel
  2. 您真的不需要致电label.sizeToFit()
  1. You are setting an IBOutlet then instantiating another MTMathUILabel with the same name
  2. 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.
    }


}

更好的解决方案如下:

  1. 在情节提要中创建一个UIView(因为MTMathUILabel实际上是一个UIView)
  2. 将此视图的类设置为MTMathUILabel
  3. 为此视图连接IBOutlet
  1. Create a UIView in storyboard (because MTMathUILabel is actually a UIView)
  2. Set this view's class to MTMathUILabel
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆