Swift get UITextField placeholder label color - ios

I want to know what color is used for text field placeholder.
I have project that use default text field with default placeholder in many times. Also i need to implement UI elements that use custom placeholder label. Unfortunately, i can't find any information about color specification of UITextField placeholder.
Can someone provide RGB or HEX value for this? Thanks.

The attributedPlaceholder property of UITextField is a NSAttributedString. Querying this string's .foregroundColor attribute yields UIExtendedSRGBColorSpace 0 0 0.0980392 0.22.
So the color seems to be:
let color = UIColor(red: 0, green: 0, blue: 0.0980392, alpha: 0.22)

Xcode can pick color from editor #_#
the color is very closed to Magnesium in Crayons panel of color picker,
hex is C7C7CD, Magnesium is C0C0C0, I like regular number, suggest you use C0C0C0 instead, really the same

Related

How to use Color with RGB or hex value in SwiftUI using Swift 5? [duplicate]

This question already has answers here:
How can I create a UIColor from a hex string?
(49 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question 12 months ago and left it closed:
Original close reason(s) were not resolved
I am currently trying to change the background color of a Label in SwiftUI. As far as I know, SwiftUI takes a View as the parameter for the background Modifier.
However, I encountered a problem I am not able to fix:
I would like to change the background color of my Label by using a hex code or an RGB value. Because of some reason, when doing so the background color always changes to white.
This works perfectly fine:
Label(
title: { Text("someTitle") },
icon: { Image(systemName: "someName") }
)
.background(Color.purple) // Using the provided standard Colors works perfectly fine.
However, I would like to create a background color using a hex or RGB value:
.background(Color(Color.RGBColorSpace.sRGB, red: 92, green: 225, blue: 230, opacity: 1))
// or
.background(Color(UIColor(red: 220, green: 24, blue: 311, alpha: 1)))
This does not work and the background color of the Label always changes back to white. How to achieve this goal?
The UIColor init method UIColor(red:green:blue:alpha) takes float values from 0 to 1 for each value.
Any value ≥ 1.0 is going to "max out" that color. So an expression like UIColor(red: 220, green: 24, blue: 311, alpha: 1) will set all 3 color channels and alpha to 1.0, resulting in white. So would UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
You could create an extension to UIColor that adds convenience initializers that would take values of different types. First, and initializer that would take values from 0-255 for each. Then you could create another intializer UIColor.init(hexString:String).
The one that took values from 0-255 would be a lot easier to write. The one that took hex strings would involve implementing a hex parser. I bet somebody has already written it though.
Take a look at this code, for example:
https://gist.github.com/anonymous/fd07ecf47591c9f9ed1a

How to set a custom background color to UIButton?

I was wondering about different types of code for changing the background colour of the button. In my project, I am using:
button4.backgroundColor = UIColor.green
But it only gives me a restricted amount of colour options since it is UIColor. I was wondering if I can write a different piece of code to have more flexibility in the color of the button.
Thanks for any help given.
But it only gives me a restricted amount of colour options since it is
UIColor.
That would be correct when working with the predefined static colors. However, no doubt that you can get another color which is none of the predefined ones.
For instance, consider you have a custom rgb color that you want to use in your app (the button background color), as: red: 100 green: 44 blue: 63, You could get it by using:
init(red:green:blue:alpha:)
Initializes and returns a color object using the specified opacity and
RGB component values.
as follows:
let myCustomRGBColor = UIColor(red: 100.0/255.0, green: 44.0/255.0, blue: 63.0/255.0, alpha: 1.0)
Note that the values are divided on 255.0 because rgb values for the UIColor are between 0 and 1.
Also, note that UIColor has many other initializers for creating a custom color object.

Print button background color

Hi is there any way to print out selected button background color as string value?? For now when i click button and try to print value it gives be something like this -:
Optional(UIExtendedSRGBColorSpace 0.870588 0.721569 0.529412 1)
can I get color name somehow?Like Green,Blue?
This what you get here is color space in RGBA format, given:
Red: 0.870588
Green: 0.721569
Blue: 0.529412
Alpha: 1
If you do some math and multiply these numbers by 255, you get:
R: 221,99994
G: 184,000095
B: 135,00006
which looks like this:
https://www.colorcodehex.com/ddb887/
I guess there is no way to the absolute name of color, except you make enum with ranges and setup colors from the ranges... but I think No one is so crazy to do this... so you have to stick with this for now :)

UIButton title color RGB values programmatically different than what was set in Interface Builder

I added a UIButton to my view. It's a regular button of system type, default config and plain title, all default values. I set the text color property in IB to the RGB values shown below: r68, g66, b62
I made a custom color programmatically with the same RGB values:
extension UIColor {
class var customColor: UIColor {
return UIColor(red: 68/255, green: 66/255, blue: 62/255, alpha: 1)
}
}
Why are the button's RGB values different when I check them in my view controller than what I set them to be in IB (identical to my custom color RGB values)?
// UIExtendedSRGBColorSpace 0.33669 0.327454 0.310221 1
myButton.titleColor(for: .normal)!.description)
// UIExtendedSRGBColorSpace 0.266667 0.258824 0.243137 1
UIColor.customColor.description
Shouldn't they be the same? If not, how do I get them to be the same?

UINavigationBar background color setted from storyboard differ from the color setted by code

My app will be available only for iOS 8 and I'm working with XCode 6.1.1.
If I set the color through the storyboard (setting theBar Tint attribute on Navigation Bar section)
The desired color is:
56 186 145
I used Core coding utilities to get the floating values of my color.
By code:
let backgroundColor = UIColor(red: 0.22, green: 0.729, blue: 0.569, alpha: 1.0)
self.navigationController!.navigationBar.barTintColor = backgroundColor
self.navigationController!.navigationBar.translucent = false
Color setted through storyboard is the same as the Original RGB.
By code:
By storyboard:
EDIT
I tried using let backgroundColor = UIColor(red: 0.255, green: 0.769, blue: 0.635, alpha: 1.0) and rgb from storyboard: 65 196 162 based on #siejkowski comment, but I get these colors:
By code:
By storyboard:
Why?
There is two reason for the color differences here you observed.
The value set in Storyboard is RGB (sRGB IEC61966-2.1) type and when you code UIColor by RGB value it will return RGB (Generic RGB) values.
So when you change color from Storyboard the values are different for RBG types. To get exact RGB value change type of RGB Sliders in Storyboard.
Click on Settings icon which is exactly right to the RGB Sliders Option. It will show a pop-up menu - select Generic RGB option.
Now you can observe an image that values for RGB
56 186 145
is now change to
49 175 126
So these are desire values to code.
Roundup issue:
In code, you are passing giving round up values for parameter like in below line
UIColor(red: 0.22, green: 0.729, blue: 0.569, alpha: 1.0)
So it will make a small change per pixel of the color code. I suggest you divide this values by 255 and leave round up calculation for the compiler. Which will gives you desire color accuracy.
So now for new values update code will be:
let backgroundColor = UIColor(red: 49.0/255.0, green: 175.0/255.0, blue: 126.0/255.0, alpha: 1.0)
self.navigationController!.navigationBar.barTintColor = backgroundColor
self.navigationController!.navigationBar.isTranslucent = false
You see the difference in colors because they are set using the same RGB values, but in different color-spaces.
UIColor, when set from code, interprets the values as from sRGB space, as provided here: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGColorSpace/index.html
However, it seems that the Storyboard color picker is using native values RGB by default. You need to convert between the two. You can do it in Storyboard color picker by clicking the settings icon next to RGB Sliders list picker in Colors tab. Native RGB values are set under Color LCD setting and sRGB values are under sRGB, of course.
When you do it, is seems that 56 186 145 in native RGB space is 73 186 141 in sRGB space. So your code needs to be:
let backgroundColor = UIColor(red: 0.286, green:0.729, blue:0.553, alpha:1.0)
When I try to change the color space to GenericRGB from the storyboard/Interface builder (on XCode 6.3.2), XCode does not save the change. So if I reopen the color from Interface builder the color space is always sRGBIEC61966-2.1.
So I came up a different solution that may be useful for someone else:
with the Mac OS default app "ColorSync Utility" I used the Calculator and translate the value of my color from GenericRGB to a sRGBIEC61966-2.1. Then I set the translated value into the Interface builder and I obtained the same result as was obtaining it programmatically!

Resources