programing

UIButton 제목 텍스트 색상은 어떻게 설정하나요?

easyjava 2023. 4. 19. 23:34
반응형

UIButton 제목 텍스트 색상은 어떻게 설정하나요?

텍스트의 색상을 버튼용으로 변경해 보았지만, 아직 흰색으로 남아 있습니다.

isbeauty = UIButton()
isbeauty.setTitle("Buy", forState: UIControlState.Normal)
isbeauty.titleLabel?.textColor = UIColorFromRGB("F21B3F")
isbeauty.titleLabel!.font = UIFont(name: "AppleSDGothicNeo-Thin" , size: 25)
isbeauty.backgroundColor = UIColor.clearColor()
isbeauty.layer.cornerRadius = 5
isbeauty.layer.borderWidth = 1
isbeauty.layer.borderColor = UIColorFromRGB("F21B3F").CGColor
isbeauty.frame = CGRectMake(300, 134, 55, 26)
isbeauty.addTarget(self,action: "first:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(isbeauty)

빨간색, 검정색, 파란색으로 변경해 봤지만 아무 일도 일어나지 않아요.

사용하셔야 합니다.func setTitleColor(_ color: UIColor?, for state: UIControl.State)실제 제목 텍스트를 설정하는 것과 같은 방법으로.문서

isbeauty.setTitleColor(UIColorFromRGB("F21B3F"), for: .normal)

Swift UI 솔루션

Button(action: {}) {
            Text("Button")
        }.foregroundColor(Color(red: 1.0, green: 0.0, blue: 0.0))

스위프트 3, 스위프트 4, 스위프트 5

코멘트를 개선합니다.이 조작은 유효합니다.

button.setTitleColor(.red, for: .normal)

단추 제목 색상 설정 예제

btnDone.setTitleColor(.black, for: .normal)

이것은 swift 5 호환 답변입니다.내장 색상 중 하나를 사용하고 싶다면 간단히 사용할 수 있습니다.

button.setTitleColor(.red, for: .normal)

커스텀 컬러를 원하시면 먼저 아래와 같이 UICollor 확장자를 만드세요.

import UIKit
extension UIColor {
    static var themeMoreButton = UIColor.init(red: 53/255, green: 150/255, blue: 36/255, alpha: 1)
}

그러면 아래와 같이 버튼에 사용하세요.

button.setTitleColor(UIColor.themeMoreButton, for: .normal)

팁: 이 방법을 사용하여 rgba 색상 코드의 사용자 지정 색상을 저장하고 응용 프로그램 전체에서 재사용할 수 있습니다.

func setTitleColor(_ color: UIColor?, 
               for state: UIControl.State)

파라미터:

색상:
지정된 상태에 사용할 제목 색상입니다.

상태:
지정된 색상을 사용하는 상태.가능한 값은 UIControl에 설명되어 있습니다.주.

샘플:

let MyButton = UIButton()
MyButton.setTitle("Click Me..!", for: .normal)
MyButton.setTitleColor(.green, for: .normal)

만약 당신이 NSAttribute 없이 버튼을 사용하고 있다면, 당신은 간단히 버튼의 제목과 색상을 설정할 수 있습니다.

yourButtonName.setTitle("Your Title", for: .normal)

enterCustomAmount.setTitleColor(.gray, for: .normal)

그러나 버튼 속성에 NSAttribute를 지정할 경우 먼저 버튼 속성을 설정해야 합니다.

var myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.init(hexString: "#FFAEA9")]

또는 여러 속성에 대해 다음과 같은 쉼표 구분 기호를 사용할 수 있습니다.

var myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.init(hexString: "#FFAEA9"), NSAttributedString.Key.font: UIFont(name: "Dubai-Medium", size: 16) ]

이렇게 UIButton에 할당하면

 let myString = "Your title"
            let text = NSAttributedString(string: myString, attributes: myAttribute)
            enterCustomAmount.setAttributedTitle(text, for: .normal)

라디오 버튼을 참조하여 다음과 같이 세그먼트 제어에서도 수행할 수 있습니다.

1단계: 세그먼트화된 컨트롤을 속성 검사기의 뷰로 끌어 "Male" 및 "Female"과 같은 두 세그먼트의 제목을 변경합니다.

순서 2: 코드로 콘센트를 작성하고 액션을 취한다.

3단계: 향후에 사용할 변수를 생성하여 선택지의 데이터를 포함시킨다.

다음과 같이 합니다.

@IBOutlet weak var genderSeg: UISegmentedControl!

var genderPick : String = ""


 @IBAction func segAction(_ sender: Any) {

    if genderSeg.selectedSegmentIndex == 0 {
         genderPick = "Male"
        print(genderPick)
    } else if genderSeg.selectedSegmentIndex == 1 {
        genderPick = "Female"
          print(genderPick)
    }
}

제목 색상 설정

btnGere.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)

16진수 코드로 UIButton 제목 색상을 설정할 수 있습니다.

btn.setTitleColor(UIColor(hexString: "#95469F"), for: .normal)

텍스트 색상을 변경하려면

button.titleLabel.textColor = UIColor.grayColor()

상태를 변경하려면 on 버튼에서 add following - 를 누릅니다.

button.enabled = true

IBAtion 메서드는 다음과 같아야 합니다.

@IBAction func buttonTapped(sender : UIButton!) {
    sender.enabled = false
}

언급URL : https://stackoverflow.com/questions/31088172/how-to-set-the-title-text-color-of-uibutton

반응형