🍎 Apple/Swift

양수 음수에 따라 textColor 변경

JINiOS 2022. 10. 12. 01:24
728x90

함수 사용

textLabel.textColor = convertDiffColor(dif: money)


func convertDiffColor(dif: Double) -> UIColor {
        guard dif > 0 else { return .systemBlue }
        return .systemRed
 }

double타입인 money를 인자로 받아 연산하여 textColor을 변환한다.

0보다 크면 systemRed를, 0보다 작으면 .systemBlue로 표시한다.

 

 

삼항연산자 사용

diffLabel.textColor = stock.diff > 0 ? .systemRed : .systemBlue
// (조건) ? (true일때) : (false일때)
728x90