[iOS/Swift] UIButton setBackgroundImage? setImage?

chaentopia·2023년 2월 18일
0

UIButton()

UIButton()의 속성에는 여러가지가 있는데, setBackgroundImage와 setImage의 차이가 궁금해서 한번 찾아보았다.

애플 공식 문서에 나온 사진으로 한 번에 이해할 수 있는데,


background는 뒤에 있는 흰 배경, Image는 버튼 옆에 있는 하늘색 아이콘을 의미한다.

사진에서 설명 되어 있듯, Image는 버튼의 전경 이미지, Background는 배경 이미지로 제목과 전경 이미지 뒤에 표시할 수 있다.

사용법

let button = UIButton()
button.setBackgroundImage(UIImage(named: "사진이름"), for: .normal)
button.setImage(UIImage(named: "시진이름"), for: .normal)

Extension으로 밑줄 만들기

extension UIButton {
    func setUnderline() {
        guard let title = title(for: .normal) else { return }
        let attributedString = NSMutableAttributedString(string: title)
        attributedString.addAttribute(.underlineStyle,
                                      value: NSUnderlineStyle.single.rawValue,
                                      range: NSRange(location: 0, length: title.count)
        )
        setAttributedTitle(attributedString, for: .normal)
    }
}

사용법은 이렇게

let button = UIButton()
button.setUnderline()

이렇게 이용한다면 UITextField에도 extension을 추가해서 밑줄 함수를 만들 수 있지 않을까 싶다!!


참고자료
https://ios-development.tistory.com/742

profile
the pale blue dot

0개의 댓글