Skip to content

Commit

Permalink
[feat] 삭제버튼 text와 Image간 변환 기능 추가, Preview extension 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
kimdj1102 committed Dec 5, 2024
1 parent 2ce7965 commit 5a834d7
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "BBBF4054-CF00-4C35-8797-88172D131DB6"
type = "1"
version = "2.0">
</Bucket>
21 changes: 21 additions & 0 deletions [1]Calculator/Assets.xcassets/deleteButton.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "free-icon-delete-2087825.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 24 additions & 3 deletions [1]Calculator/Controller/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,53 @@ import UIKit
class ViewController: UIViewController {

@IBOutlet var numberLabel: UILabel!

@IBOutlet var deleteButton: UIButton!

var textManager = TextChangeManager()
var imageChangeManager = ImageChangeManager()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

setup()

numberLabel.text = "0"
}

func setup() {
numberLabel.lineBreakMode = .byTruncatingHead
numberLabel.text = "0"

}

@IBAction func numberBtnPressed(_ sender: UIButton) {
let number = sender.tag - 10
guard let label = numberLabel.text else { return }

numberLabel.text = textManager.addText(label, number)
imageChangeManager.changeImage(deleteButton, numberLabel)
}

@IBAction func deleteBtnPressed(_ sender: UIButton) {
guard let label = numberLabel.text else { return }

numberLabel.text = textManager.removeText(label)
imageChangeManager.changeImage(deleteButton, numberLabel)
}
}

// MARK: - Preview

#if DEBUG
import SwiftUI

#Preview("Sample") {
let storyboard = UIStoryboard(name: "Main", bundle: nil)

let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController

vc.toPreview()
}
#endif



49 changes: 49 additions & 0 deletions [1]Calculator/Model/Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Extensions.swift
// [1]Calculator
//
// Created by 김동준 on 12/5/24.
//

import UIKit
import SwiftUI

extension UIImage {
func resize(size: CGSize) -> UIImage? {
// 비트맵 생성
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
// 비트맵 그래픽 배경에 이미지 다시 그리기
self.draw(in: CGRect(origin: CGPoint.zero, size: size))
// 현재 비트맵 그래픽 배경에서 이미지 가져오기
guard let resizedImage = UIGraphicsGetImageFromCurrentImageContext() else {
return nil
}
// 비트맵 환경 제거
UIGraphicsEndImageContext()
// 크기가 조정된 이미지 반환
return resizedImage
}
}

// MARK: - SwiftUI Preview

#if DEBUG
import SwiftUI

extension UIViewController {
private struct Preview: UIViewControllerRepresentable {
let viewController: UIViewController

func makeUIViewController(context: Context) -> UIViewController {
return viewController
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}

func toPreview() -> some View {
Preview(viewController: self)
}
}
#endif
24 changes: 24 additions & 0 deletions [1]Calculator/Model/ImageChangeManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// ImageChangeManager.swift
// [1]Calculator
//
// Created by 김동준 on 12/5/24.
//

import UIKit

struct ImageChangeManager {
func changeImage(_ button: UIButton, _ label: UILabel) {
let originalImage = UIImage(named: "deleteButton")
let changedImage = originalImage?.resize(size: CGSize(width: 50, height: 50))?.withTintColor(.white)

if label.text != "0" {
button.setTitle("", for: .normal)
button.setImage(changedImage, for: .normal)
} else {
button.setTitle("AC", for: .normal)
button.setImage(nil, for: .normal)
}
}
}

3 changes: 2 additions & 1 deletion [1]Calculator/View/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="_1_Calculator" customModuleProvider="target" sceneMemberID="viewController">
<viewController storyboardIdentifier="ViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="BYZ-38-t0r" customClass="ViewController" customModule="_1_Calculator" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="NLf-O5-JjO">
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
Expand Down Expand Up @@ -311,6 +311,7 @@
</constraints>
</view>
<connections>
<outlet property="deleteButton" destination="2yh-I3-bc3" id="Ljl-dP-eYk"/>
<outlet property="numberLabel" destination="n9E-XH-EOd" id="Z55-Bq-mx8"/>
</connections>
</viewController>
Expand Down

0 comments on commit 5a834d7

Please sign in to comment.