TextView borderColor Not Changing - ios

I am trying to change the borderColor of a textView in Swift. If I set it using a preset color it works fine:
inView.layer.borderColor = UIColor.green.cgColor
However, when I try to set it to a custom RGB value, the borderColor does not change:
inView.layer.borderColor = UIColor.init(red: 100, green: 230, blue: 100, alpha: 1).cgColor

You need to construct color properly ($value/255.0) as color range is from 0 to 1 ( aka 0 to 255/255 )
inView.layer.borderColor = UIColor(red: 100/255.0, green: 230/255.0, blue: 100/255.0, alpha: 1).cgColor

Related

Gradient color as a Background color of UIButton in Swift4

I have a button and i'm trying to set its background color gradient.I have three hex values which i converted to RGB and written three values in my code and pass all the three values to the button frame. But when i run the app button background not changes according to the color i have set in the code. My code to get three color codes is this,
let gradientColor = CAGradientLayer()
gradientColor.frame = loginButton.frame
let color1 = UIColor(red: 183, green: 46, blue: 79, alpha: 1)
let color2 = UIColor(red: 232, green: 79, blue: 80, alpha: 1)
let color3 = UIColor(red: 145, green: 21, blue: 79, alpha: 1)
gradientColor.colors = [color1,color2,color3]
self.loginButton.layer.addSublayer(gradientColor)
Try this and see:
#IBOutlet weak var loginButton: UIButton!
func testGradientButton() -> Void {
let gradientColor = CAGradientLayer()
gradientColor.frame = loginButton.frame
gradientColor.colors = [UIColor.blue.cgColor,UIColor.red.withAlphaComponent(1).cgColor]
self.loginButton.layer.insertSublayer(gradientColor, at: 0)
}
Here is result:
Also note: Values for RGB colors in your code, are much higher than its normal value. See your console log, it might printed following error:
[Graphics] UIColor created with component values far outside the expected range. Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once.
Divide all values with 255.0 and use insertSubLayer function.
let color1 = UIColor(red: 183.0/255.0, green: 46.0/255.0, blue: 79.0/255.0, alpha: 1)
let color2 = UIColor(red: 232.0/255.0, green: 79.0/255.0, blue: 80.0/255.0, alpha: 1)
let color3 = UIColor(red: 145.0/255.0, green: 21.0/255.0, blue: 79.0/255.0, alpha: 1)
Here is result with your color code values:
func testGradientButton() -> Void {
let gradientColor = CAGradientLayer()
gradientColor.frame = loginButton.frame
let color1 = UIColor(red: 183.0/255.0, green: 46.0/255.0, blue: 79.0/255.0, alpha: 1)
let color2 = UIColor(red: 232.0/255.0, green: 79.0/255.0, blue: 80.0/255.0, alpha: 1)
let color3 = UIColor(red: 145.0/255.0, green: 21.0/255.0, blue: 79.0/255.0, alpha: 1)
gradientColor.colors = [color1.cgColor,color2.cgColor,color3.cgColor]
self.loginButton.layer.insertSublayer(gradientColor, at: 0)
}

Using Custom Color with iOS Charts labelTextColor

Does anyone know how to get a custom color to work with labelTextColor in iOS Charts? When I try to plug in a custom color the labels just disappear. For example:
This works:
self.chartView.leftAxis.labelTextColor = UIColor.cyanColor()
But something like this does not:
self.chartView.rightAxis.labelTextColor = UIColor(red: 150, green: 202, blue: 56, alpha: 1)
replace
self.chartView.rightAxis.labelTextColor = UIColor(red: 150, green: 202, blue: 56, alpha: 1)
with
self.chartView.rightAxis.labelTextColor = UIColor(red: 150.0/255, green: 202.0/255, blue: 56.0/255, alpha: 1)

UITabBar icons text color

How can I change the text color of UITabBar (which is contained in UINavigationController if it's matters) ?
If I write this -
UITabBar.appearance().tintColor = UIColor.greenColor()
then the color would be green as expected, but if I write this -
UITabBar.appearance().tintColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0)
The text becomes invisible
The same goes for
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected) //red color
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: CGFloat(104), green: CGFloat(154), blue: CGFloat(26), alpha: CGFloat(1))], forState:.Selected) //invisible
Please try writing it like this:
UITabBar.appearance().tintColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
You must divide the red, green and blue by 255, like this
UIColor(red:255/255.0, green:255/255.0, blue: 255/255.0, alpha: 1.0)

How to set custom (RGB) color to Navigation bar?

I cannot to set my RGB color to UINavigationBarand ToolBar. I tried it
let myColor = UIColor(red: 47, green: 206, blue: 255, alpha: 1.0)
self.navigationController?.navigationBar.barTintColor = myColor
self.navigationController?.toolbar.tintColor = myColor
Also I tried HSB color
let secondColor = UIColor(hue: 194, saturation: 82, brightness: 100, alpha: 1.0)
self.navigationController?.navigationBar.barTintColor = secondColor
self.navigationController?.toolbar.tintColor = secondColor
But when I wrote the following method it works.
self.navigationController?.navigationBar.barTintColor = UIColor.greenColor()
self.navigationController?.toolbar.tintColor = UIColor.greenColor()
How can I set my RGB color to bars?
let myColor = UIColor(red: 47, green: 206, blue: 255, alpha: 1.0)
is incorrect. UIColor requires color components in a range 0.0 ... 1.0. So you probably need
let myColor = UIColor(red: 47.0/255.0, green: 206.0/255.0, blue: 255.0/255.0, alpha: 1.0)
From the UIColor docs
UINavigationBar.appearance().barTintColor = UIColor(red: 73.0 / 255.0, green: 155.0 / 255.0, blue: 255.0/ 255.0, alpha: 1.0)

change border color on custom color

I v got UITextView, and I want to set it a new custom color like this
var instaColor = UIColor(red: 51, green: 92, blue: 131, alpha: 1).CGColor
textView.layer.borderWidth = 1
textView.layer.cornerRadius = 20
textView.layer.borderColor = instaColor
but its color is white or not appear at all, whats with this? this color should be dark blue + green
UIColor requires the color values to be between 0 and 1 so you should change it to:
var instaColor = UIColor(red: 51/255, green: 92/255, blue: 131/255, alpha: 1).CGColor
In the above you are dividing each value by 255 to get the color value to between 0 and 1.
I think your code should work after you do that.
Thanks

Resources