[ios-TIL] UI๊ณต๋ถ€(Label&Button)

๊ฐ์ž๋งจยท2022๋…„ 8์›” 2์ผ
0

ios

๋ชฉ๋ก ๋ณด๊ธฐ
1/6
post-thumbnail

Today what i learned !

๐Ÿ’ป ๐Ÿ“’๐Ÿ“• ๐Ÿ“—๐Ÿ“˜๐Ÿ“™๐Ÿ“š๐Ÿ“–

label

class ViewController: UIViewController {


    
    override func viewDidLoad() {
        super.viewDidroad()  
    // ์• ํ”Œ์ด ๋งŒ๋“ค์–ด ๋†“์€ ์ž๋™ํ•จ์ˆ˜.
    ๋ทฐ๊ฐ€ ๋กœ๋“œ ๋˜๋ฉด์€ ํ•จ์ˆ˜ ์•ˆ์— ์‹คํ–‰๋ฌธ์ด ์‹คํ–‰๋จ.
        
    }
}

ํ™”๋ฉด ๋’ท๋ฐฐ๊ฒฝ ์ƒ‰์ƒ ์ž…ํžˆ๊ธฐ

class ViewController: UIViewController {


    
    override func viewDidLoad() {
        super.viewDidroad() 
        view.backgroundColor = .green
        // ํ™”๋ฉด ๋’ท์ปฌ๋Ÿฌ ๋งŒ๋“ค๊ธฐ 
    }
}  
        

label

command + shift + l ๋‹จ์ถ•ํ‚ค๋ฅผ ์ด์šฉํ•˜์—ฌ label์„ ์ฐพ์•„ ๋ฐ”๋กœ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•๊ณผ
์ฝ”๋“œ๋กœ label ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

class ViewController: UIViewController {

    let label = UILabel()
    // uilabel๋งŒ๋“ค๊ธฐ
    override func viewDidLoad() {
        super.viewDidroad() 
        view.backgroundColor = .green
        view.addSubview(label) //์ด ์ƒํƒœ๋Š” ๊ธ€์ž๋ฅผ ์•ˆ๋„ฃ์–ด์ค˜์„œ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค.
        label.text = "yjpotato" //์ด๋ž˜๋„ ๊ธ€์ž๊ฐ€ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค. ์ด์œ ๋Š” ์œ„์น˜๋ฅผ ์•ˆ์ ์–ด์คฌ๊ธฐ ๋•Œ๋ฌธ์—!
        label.frame = CGRect (x: view.frame.midX,
                              y: view.frame.midY,
                              width: 200,
                              height: 100)
        label.backgroundColor = .red      //label ๋’ค์— ์ƒ‰์ƒ ์ž…ํžˆ๊ธฐ. ์ด๋•Œ ์œ„์น˜๊ฐ€ ์ •๊ฐ€์šด๋ฐ๊ฐ€ ์•„๋‹Œ๋‹ค. ๋‹ค์‹œ ์œ„์น˜ ์ •ํ•ด์ฃผ๊ธฐ!    
    }
}                  
class ViewController: UIViewController {

    let label = UILabel()
    // uilabel๋งŒ๋“ค๊ธฐ
    override func viewDidLoad() {
        super.viewDidroad() 
        view.backgroundColor = .green
        view.addSubview(label) 
        label.text = "yjpotato" 
        label.frame = CGRect (x: view.frame.midX - 100,
                              y: view.frame.midY - 50,
                              width: 200,
                              height: 100) // label๋’ค์˜ ๋ฐ•์Šค๊ฐ€ ๊ฐ€์šด๋ฐ์— ์™”์ง€๋งŒ ๊ธ€์ž๋Š” ๊ฐ€์šด๋ฐ๊ฐ€ ์•„๋‹ˆ๋‹ค.
        label.backgroundColor = .red     
        label.textAlignment = .center  // ๊ธ€์ž๊ฐ€ ๊ฐ€์šด๋ฐ ์™”๋‹ค!
    }
}  

label๋’ค ๋ฐ•์Šค ๊ฐ€์šด๋ฐ ์ •๋ ฌ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

class ViewController: UIViewController {

    let label = UILabel()
    // uilabel๋งŒ๋“ค๊ธฐ
    override func viewDidLoad() {
        super.viewDidroad() 
        view.backgroundColor = .green
        view.addSubview(label) 
        label.text = "yjpotato" 

        let width: CGFloat = 200    // label๋’ค ๋ฐ•์Šค ๊ฐ€์šด๋ฐ ์ •๋ ฌ ์ƒˆ๋กœ์šด ๋ฐฉ๋ฒ•
        let height: CGFloat = 100
        label.frame = CGRect (x: view.frame.midX - width / 2,
                              y: view.frame.midY - height / 2,
                              width: width,
                              height: height) 
        label.backgroundColor = .red     
        label.textAlignment = .center  
    }
}        

Button

button ๋งŒ๋“ค๊ธฐ

class ViewController: UIViewController {

    let button = UIButton(type: .system)
    
    override func viewDidLoad() {
        super.viewDidroad() 
        view.backgroundColor = .green
        view.addSubview(button) 
        button.setTitle("๋ฒ„ํŠผ", for: .normal) // setTitle์€ button์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ.

        let width: CGFloat = 200    
        let height: CGFloat = 100
        button.frame = CGRect (x: view.frame.midX - width / 2,
                              y: view.frame.midY - height / 2,
                              width: width,
                              height: height) 
        button.backgroundColor = .red     
    }
}         

์‹œ์Šคํ…œํšจ๊ณผ ๋ณด๊ธฐ. ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฌ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ (์œ„์˜ ๋ฒ„ํŠผ ์ƒ‰์ƒ ์—†์•ฐ.)

class ViewController: UIViewController {

    let button = UIButton() // ๋ฒ„ํŠผ์ด ๋ณด์ด์ง€ ์•Š์ง€๋งŒ ์žˆ์Œ.
    
    override func viewDidLoad() {
        super.viewDidroad() 
        view.addSubview(button) 
        button.setTitle("๋ฒ„ํŠผ", for: .normal) 
        button.setTitleColor(.red, for: .normal)

        let width: CGFloat = 200    
        let height: CGFloat = 100
        button.frame = CGRect (x: view.frame.midX - width / 2,
                              y: view.frame.midY - height / 2,
                              width: width,
                              height: height) 
            
    }
}         

๋ฒ„ํŠผ์— ์•ก์…˜ ๋„ฃ์–ด์ฃผ๊ธฐ

class ViewController: UIViewController {

    let button = UIButton(type: .system)
    
    override func viewDidLoad() {
        super.viewDidroad() 
        view.backgroundColor = .green
        view.addSubview(button) 
        button.setTitle("๋ฒ„ํŠผ", for: .normal) 
        //๋ฒ„ํŠผ์— ์•ก์…˜ ๋„ฃ์–ด์ฃผ๊ธฐ
        button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside) //#selector๋ฅผ ์ ์–ด์•ผ ํ•˜๋Š” ์ด์œ ๋Š” ๋ฒ„ํŠผ์— ์•ก์…˜์„ ์ „๋‹ฌํ• ๋•Œ objectc๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•˜๊ธฐ๋•Œ๋ฌธ.
        button.frame.size = CGSize(width: 100, height: 100) // ๋ฒ„ํŠผ ์‚ฌ์ด์ฆˆ ์žก์•„์ฃผ๊ธฐ


        let width: CGFloat = 200    
        let height: CGFloat = 100
        button.frame = CGRect (x: view.frame.midX - width / 2,
                              y: view.frame.midY - height / 2,
                              width: width,
                              height: height) 
            
    }
    @objc //selector ์•ˆ์—๋Š” @objc๊ฐ€ ๋“ค์–ด๊ฐ„ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ค˜์•ผํ•จ
    func didTapButton(_ sender: UIButton){  // _ sender ์ƒ๋žต๊ฐ€๋Šฅ. ์•ˆ์—์„œ sender์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ๋ถ„์ด ๊ฐ€๋Šฅ(๋ฒ„ํŠผ์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ๊ฒฝ์šฐ)
        print("Button Tapped")
    }
}         
profile
๋‚˜๋Š” ์ฝ”๋”ฉํ•˜๋Š” ๊ฐ์ž๋‹ค!

0๊ฐœ์˜ ๋Œ“๊ธ€