85

When I try setting the color of a UILabel to the color of another UILabel using the code

myLabel.textColor = otherLabel.textColor

It doesn't change the color. When I use this code, however,

myLabel.textColor = UIColor.redColor()

It changes the color correctly. What's the issue with the first line?

kag359six
  • 1,693
  • 2
  • 16
  • 21
  • 4
    There is no issue with the first line. otherLabel.textColor must not be what you think it is at this point in your app's execution. – Mick MacCallum Sep 14 '14 at 19:38
  • 1
    Hi kag, this post was helpful to me, in particular @Mayank Patel's answer. If any of these answers worked out for you, could you by chance select an answer to make finding the best option quicker for others. Thanks! – Dan Beaulieu Jun 11 '15 at 00:12

10 Answers10

73

The easiest workaround is create dummy labels in IB, give them the text the color you like and set to hidden. You can then reference this color in your code to set your label to the desired color.

yourLabel.textColor = hiddenLabel.textColor

The only way I could change the text color programmatically was by using the standard colors, UIColor.white, UIColor.green...

fernandosavio
  • 9,849
  • 4
  • 24
  • 34
Piet Grootnoten
  • 982
  • 6
  • 3
  • You should be able to create custom colors: [UIColor](https://developer.apple.com/documentation/uikit/uicolor/1621925-init) – Erik P. Feb 07 '18 at 20:57
51

This code example that follows shows a basic UILabel configuration.

let lbl = UILabel(frame: CGRectMake(0, 0, 300, 200))
lbl.text = "yourString"

// Enum type, two variations:
lbl.textAlignment = NSTextAlignment.Right
lbl.textAlignment = .Right

lbl.textColor = UIColor.red
lbl.shadowColor = UIColor.black
lbl.font = UIFont(name: "HelveticaNeue", size: CGFloat(22))
self.view.addSubview(lbl)
Nyakiba
  • 862
  • 8
  • 18
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
31

I don't know why but to change the text color of the labels you need to divide the value you want with 255, because it works only until 1.0.

For example a dark blue color:

label.textColor = UIColor(red: 0.0, green: 0.004, blue: 0.502, alpha: 1.0)
loopidio
  • 423
  • 1
  • 5
  • 11
  • 1
    Thank you - that's exactly what I was looking for because that little app that I got (Color Picker) shows the 255 and I can set the color to any color I want. Perfect match :) Swift 4 btw. – Aeger Jun 24 '18 at 21:53
9

Made an app with two labels in IB and the following:

@IBOutlet var label1: UILabel!
@IBOutlet var label2: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    label1.textColor = UIColor.redColor() // in Swift 3 it's UIColor.red
    label2.textColor = label1.textColor
}

label2 color changed as expected, so your line works. Try println(otherLabel.textColor) right before you set myLabel.textColor to see if the color's what you expect.

mfaani
  • 33,269
  • 19
  • 164
  • 293
terrafirma9
  • 362
  • 2
  • 15
4

solution for swift 3 -

let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
titleLabel.text = "change to red color"
titleLabel.textAlignment = .center
titleLabel.textColor = UIColor.red
Himanshu
  • 740
  • 5
  • 6
3

If you are using Xcode 8 and swift 3. Use the following way to get the UIColor

label1.textColor = UIColor.red
label2.textColor = UIColor.black
Asfand Shabbir
  • 1,234
  • 13
  • 9
2

I think most people want their placeholder text to be in grey and appear only once, so this is what I did:

  1. Set your color in viewDidLoad() (not in IB)

    commentsTextView.textColor = UIColor.darkGray
    
  2. Implement UITextViewDelegate to your controller

  3. add function to your controller

    func textViewDidBeginEditing(_ textView: UITextView) {
        if (commentsTextView.textColor == UIColor.darkGray) {
           commentsTextView.text = ""
           commentsTextView.textColor = UIColor.black
        }
    }
    

This solution is simple.

Frank
  • 7,235
  • 9
  • 46
  • 56
1

You can use as below and also can use various color just assign

myLabel.textColor = UIColor.yourChoiceOfColor

Ex:

Swift

myLabel.textColor = UIColor.red

Objective-C

[myLabel setTextColor:[UIColor redColor]];

or you can click here to Choose the color,

https://www.ralfebert.de/ios-examples/uikit/swift-uicolor-picker/

Luckabo
  • 74
  • 5
Roy shetty
  • 33
  • 4
0

The text field placeholder and the "is really" label is hard to see at night. So i change their color depending one what time of day it is.

Also make sure you connect the new IBOutlet isReallyLabel. To do so open Main.storybaord and control-drag from "Convert" view controller to the "is really" text field and select the isReallyLabel under Outlets.

WARNING: I have not tested to see if the application is open while the time of day swaps.

@IBOutlet var isReallyLabel: UILabel! 
  
override func viewWillAppear(animated: Bool) {
 let calendar = NSCalendar.currentCalendar()
 let hour = calendar.component(.Hour, fromDate: NSDate())
  
 let lightColor = UIColor.init(red: 0.961, green: 0.957, blue: 0945, alpha: 1)
 let darkColor = UIColor.init(red: 0.184, green: 0.184 , blue: 0.188, alpha: 1)

  
 switch hour {
 case 8...18:
  isReallyLabel.textColor = UIColor.blackColor()
  view.backgroundColor = lightColor
 default:
  let string = NSAttributedString(string: "Value", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])
  textField.attributedPlaceholder = string
  isReallyLabel.textColor = UIColor.whiteColor()
  view.backgroundColor = darkColor
 }
}
Jeff
  • 1
0

To change the text colour of UILable at runtime use NSAttributedText and do not set UILable.textColor.

let font = UIFont(name: "SFProText-Semibold", size: 16)!
if let messageToDisplay = currentUser?.lastMessage {

    let attributedString = NSAttributedString(string: messageToDisplay, attributes: [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: UIColor(named: "charcoal")!])
    lastMessageLabel.attributedText = attributedString
} else {
    let attributedString = NSAttributedString(string: "Start a conversation", attributes: [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: UIColor(named: "ocean")!])
    lastMessageLabel.attributedText = attributedString
}

Note charcoal and ocean are colours defined in Assets.xcassets. Resultant Label Images:

Resultant Label Images

Above code worked well for me in Xcode 10.2.1 and Swift 5.

Pankaj Kulkarni
  • 553
  • 4
  • 12