gpt4 book ai didi

ios - 标签中的图片(Swift)

转载 作者:可可西里 更新时间:2023-11-01 01:59:58 25 4
gpt4 key购买 nike

我有在计算器中显示历史记录的代码,但符号(+、-、×、÷)取自“案例”(照片 1)

怎样才能让历史上的符号(+、-、×、÷)按照我设置的图片显示(图2)

**Photo 1**

**Photo 2**

@IBAction func equalitySignPressed(sender: UIButton) {
if stillTyping {
secondOperand = currentInput
}
dotIsPlaced = false

addHistory(text: operationSign + displayResultLabel.text!)

switch operationSign {

case "+":
operateWithTwoOperands{$0 + $1}
case "-":
operateWithTwoOperands{$0 - $1}
case "×":
operateWithTwoOperands{$0 * $1}
case "÷":
operateWithTwoOperands{$0 / $1}
default: break
}
}

历史:

func addHistory(text: String){
//Add text
resultLabelText.text = resultLabelText.text! + "" + text
}

最佳答案

您可以制作您的符号图像并使用 NSTextAttachment 构建一个 NSAttributedAtring,它用带有符号图像的相应 NSTextAttachment 替换字符串中的文本。这是一个使用一张图片的 Playground 示例,但您可以轻松地将更多图片添加到字典中,以用图片替换所有其他符号:

import PlaygroundSupport
import UIKit

class V: UIViewController {
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(label)
label.textColor = .red

}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let expression = "5 + 5"
let plusAttachment = NSTextAttachment()
plusAttachment.image = UIImage(named: "star.png")
let plusString = NSAttributedString(attachment: plusAttachment)
let substitutions: [Character: NSAttributedString] = ["+": plusString]
let attributedExpression = NSMutableAttributedString()
for character in expression {
if let substitution = substitutions[character] {
attributedExpression.append(substitution)
} else {
attributedExpression.append(NSAttributedString(string: String(character)))
}
}
label.attributedText = attributedExpression
label.sizeToFit()

}
}

PlaygroundPage.current.liveView = V()

关于ios - 标签中的图片(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47419657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com