Reputation: 2038
I want to set the color of my text in my UITextField. The textborder is black but the color on the inside is transparent. How do I add color to it?
Here is what I have:
let memeTextAttributes:[String:Any] = [
NSStrokeColorAttributeName: UIColor.black,
NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
NSStrokeWidthAttributeName: 3.0]
topText.delegate = self
topText.defaultTextAttributes = memeTextAttributes
topText.text = "TOP"
topText.textAlignment = NSTextAlignment.center
topText.adjustsFontSizeToFitWidth = true
topText.center.x = self.view.center.x
topText.center.y = self.view.frame.origin.y + 150
topText.minimumFontSize = 10
topText.textColor = UIColor.white
Upvotes: 1
Views: 880
Reputation: 2038
I had to do an insane amount of research. Here is what i ended up with:
let memeTextAttributes:[String:Any] = [
NSStrokeColorAttributeName: UIColor.black,
NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
NSStrokeWidthAttributeName: -5.0
]
NSStrokeWidthAttributeName
had to be a NEGATIVE number/float to fill in the color, not a POSITIVE number/float! A positive float made the color transparent.
My project - https://github.com/ryanwaite28/ios-developer/blob/master/Meme%20Me/MemeMe2/MemeCreateEditController.swift
Took me the whole day.
Wowzerz...
Upvotes: 2
Reputation: 2766
Sorry I misunderstood what you wanted. If you want the background to be transparent, the text border color to be black, and the text color to be white, you should 1) set the text field's backgroundColor
property to .clear
, 2) set the borderColor
property of the text field's layer
property to be UIColor.black.cgColor
, 3) set the borderWidth
property of the text field's layer
property to be 1
, and 4) set the text field's textColor
property to .white
.
topText.backgroundColor = .clear
topText.layer.borderColor = UIColor.black.cgColor
topText.layer.borderWidth = 1
topText.textColor = .white
Upvotes: -1