Drawing labels using iosMath library

痴心易碎 提交于 2021-02-11 12:32:33

问题


I'm trying to use iosMath but it shows nothing. How can I use the iosMath library to draw math labels?

https://github.com/kostub/iosMath

iosMath is a library for displaying beautifully rendered math equations in iOS and MacOS applications. It typesets formulae written using the LaTeX in a UILabel equivalent class. It uses the same typesetting rules as LaTeX and so the equations are rendered exactly as LaTeX would render them.

I installed pod file.

import UIKit
import Foundation
import CoreGraphics
import QuartzCore
import CoreText
import iosMath


class Deneme_VC: 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.
    }

}

enter image description here


回答1:


I assume this is somewhere in that library's docs, but...

You need to either give the label an explicit frame, or call sizeToFit():

import UIKit

import CoreGraphics
import QuartzCore
import CoreText
import iosMath

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        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)
        
        // need to call sizeToFit() to get it to calculate the frame
        label.sizeToFit()
        
        // center it in the view
        label.center = view.center

        // so we can see the frame
        label.backgroundColor = .yellow
        
    }

}

Result:



来源:https://stackoverflow.com/questions/63638064/drawing-labels-using-iosmath-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!