如何在Xcode中把分数设计成图像或其他功能?

问题描述 投票:0回答:1

我是一个新手程序员,我现在正在做一个计算器.对于目前的情况,我可以从按钮上输入一个数字并计算它们。我需要一个光标,所以我的工作是一个显示为 UITextView. 我想从数字按钮导入数字,改变分数框中的数字,并在分数框中移动光标和其他数字。而且我想把分数框设计成这样的图像。

我怎么能把分数设计成图像或其他功能?我可以在Xcode中实现这个功能吗?如果Xcode不能工作,请告诉我哪个工具可以工作.对不起,但我不想要其他类似"³₄"的答案。

fraction

ios uitextview
1个回答
1
投票

如果你想要一个简单的分数的图像,你可以像这样渲染它。

func fraction(numerator: String, denominator: String, points: CGFloat, color: UIColor = .black) -> UIImage {
    let attributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.systemFont(ofSize: points),
        .foregroundColor: color
    ]
    let numeratorSize = (numerator as NSString).size(withAttributes: attributes)
    let denominatorSize = (denominator as NSString).size(withAttributes: attributes)
    let width = max(numeratorSize.width, denominatorSize.width)
    let spacing = points * 0.5
    let height = numeratorSize.height + denominatorSize.height + spacing
    let size = CGSize(width: width, height: height)
    return UIGraphicsImageRenderer(size: size).image { _ in
        // draw numerator

        (numerator as NSString).draw(at: CGPoint(x: (width - numeratorSize.width) / 2, y: 0), withAttributes: attributes)

        // draw denominator

        (denominator as NSString).draw(at: CGPoint(x: (width - denominatorSize.width) / 2, y: numeratorSize.height + spacing), withAttributes: attributes)

        // draw fraction line

        let path = UIBezierPath()
        color.setStroke()
        let y = numeratorSize.height + spacing / 2
        path.move(to: CGPoint(x: 0, y: y))
        path.addLine(to: CGPoint(x: width, y: y))
        path.stroke()
    }
}

因此,下面的...

let image = fraction(numerator: "42", denominator: "1000", points: 24, color: .red)

... 产生。

enter image description here


但如果你想用光标来编辑它 你可以考虑用一系列单独的控件来编辑它 例如,这是一个水平堆栈视图,其中有数字的文本字段,有数字的标签,有数字的标签。+ 符号。分数是一个垂直的堆栈视图,同样有数字的文本字段和一个1pt高的 UIView 的分数线,将分子和分母分开。

enter image description here

你不需要文字栏的边框,但它只是让你更容易看到上图中的情况。

© www.soinside.com 2019 - 2024. All rights reserved.