B2Fq
B2Fq

Reputation: 916

Pictures in the label (Swift)

I have code to display the history in the calculator but the signs (+, -, ×, ÷) are taken from the "case" (Photo 1)

How can I make it so that in the history the signs (+, -, ×, ÷) are displayed by the pictures I have set (Photo 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
    }
}

History:

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

Upvotes: 2

Views: 360

Answers (2)

Josh Homann
Josh Homann

Reputation: 16327

You can make your symbols images and use NSTextAttachment to construct a NSAttributedAtring that replaces the text in your string with the corresponding NSTextAttachment with your symbol image. Here is an example playground that does it with one image, but you can easily add more images to the dictionary to replace all of the other symbols with images:

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()

Upvotes: 1

Sweeper
Sweeper

Reputation: 271830

I suggest you get an emoji font that displays mathematical signs with a border around them. Use that font to create NSAttributedStrings and set it as the label's attributedText.

As to how to use custom fonts, you can refer to here or just search on SO. There are lots of questions about this topic.

I also see that you want the text to be bold, that can be done with attributed strings as well.

Alternatively, you can add those cool math signs as attachments to NSAttributedStrings, but I doubt it's easy to get the sizes correct.

Upvotes: 0

Related Questions