How to set the border of UITextView to same as border color of UITextField

IosSwiftUitextview

Ios Problem Overview


By default UITextField has a light gray color as its border color. I want to set my UITextView to have the same border color as the UITextField.

I tried:

myTextView.layer.borderColor = UIColor.lightGrayColor().CGColor
// or
myTextView.layer.borderColor = UIColor.lightTextColor().CGColor
// or 
myTextView.layer.borderColor = myTextField.layer.borderColor

They still have up to have different color.

How to set the UITextView border to match UITextField color?

Ios Solutions


Solution 1 - Ios

Try this code.

UIColor *borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0];

myTextView.layer.borderColor = borderColor.CGColor;
myTextView.layer.borderWidth = 1.0;
myTextView.layer.cornerRadius = 5.0;

change borderWidth and cornerRadius value to get exact ui as UITextField.

Solution 2 - Ios

This Swift code also works for setting same border color, width & radius as UITextField:

myTextView.layer.borderColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0).CGColor
myTextView.layer.borderWidth = 1.0
myTextView.layer.cornerRadius = 5

Solution 3 - Ios

I had this similar question for iOS 10, and wasn't able to find an answer, but this is what I found using the Digital Color Meter in Utilities and the iOS Simulator.

Swift 3 / iOS 10

let color = UIColor(red: 186/255, green: 186/255, blue: 186/255, alpha: 1.0).cgColor
myTextView.layer.borderColor = color
myTextView.layer.borderWidth = 0.5
myTextView.layer.cornerRadius = 5

Solution 4 - Ios

swift 4.x/ios11.

I did another measure in PSD using simulator. I can confirm radius is 0.5 and color is 0.8, as 205/255 = 0.8 (or "cdcdcd" in HEX, as PSD suggests, BUT width must be 0.5. (I attached a PSD where You can compare radius of edit field (UITExtField) AND radius applied to a UITextView.

So its correct:

	let borderGray = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1)
	self.TxtV.layer.borderColor = borderGray.cgColor
	self.TxtV.layer.borderWidth = 0.5
	self.TxtV.layer.cornerRadius = 5

Note: I tried to get color from a TextField already on View, but I got:

if let borderGray = self.cellPhoneTxt.layer.borderColor{ let BG = UIColor(cgColor: borderGray)

	print(BG)
	var red: CGFloat = 0
	var green: CGFloat = 0
	var blue: CGFloat = 0
	var alpha: CGFloat = 0
	BG.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
	print(red, green, blue, alpha)

}

but I got in console:

kCGColorSpaceModelRGB 0 0 0 1

0.0 0.0 0.0 1.0

so it seems Apple is using full black AND some Alpha.

enter image description here

Solution 5 - Ios

Try this code for Swift

let borderColor = UIColor.whiteColor.Cgcolor()

myTextView.layer.borderColor = borderColor.CGColor;
myTextView.layer.borderWidth = 1.0;
myTextView.layer.cornerRadius = 5.0;

Solution 6 - Ios

As with Swift 5.2 and with dark mode support :)

I only wanted to add an example with semantic UI using adaptable colors. Sometimes hardcoding the values is not what you want especially if you support dark mode. I think using .label as semantic color gets you to the default gray used by the UITextField border in light and dark mode. Try this out:

myTextView.layer.cornerRadius = 10
myTextView.layer.borderWidth = 1
myTextView.layer.borderColor = UIColor.label.cgColor

I like to play with other semantic colors too. This will look nice in dark mode:

myTextField.layer.cornerRadius = 10
myTextField.layer.borderWidth = 1
myTextField.layer.borderColor = UIColor.systemGray4.cgColor

and your UITextView will look exactly the same with:

myTextView.layer.cornerRadius = 10
myTextView.layer.borderWidth = 1.0
myTextView.layer.borderColor = UIColor.systemGray4.cgColor

Solution 7 - Ios

The exact color is:

Objective-C:

[UIColor colorWithRed:0.76 green:0.76 blue:0.76 alpha:1.0].CGColor;

Swift:

UIColor(red:0.76, green:0.76, blue:0.76, alpha:1.0).CGColor

Solution 8 - Ios

For generic solution through out the project. You can extern UIView class and add these methods to it.

-(void)setBorderColor:(UIColor *)borderColor{
    self.layer.borderColor = (borderColor).CGColor;
}
-(UIColor*)borderColor{
    return [UIColor colorWithCGColor: self.layer.borderColor];
}

//Getter and setter for border width
-(void)setBorderWidth:(NSInteger)borderWidth{
    self.layer.borderWidth = borderWidth;
}
-(NSInteger)borderWidth{
    return  (NSInteger)roundf((self.layer.borderWidth));
}

Its an extension to UIView. Just add these UIView+Designable.h/m files in your project and you can see the multiple more options in attribute inspector.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionn179911View Question on Stackoverflow
Solution 1 - IosDeepakView Answer on Stackoverflow
Solution 2 - IosAksView Answer on Stackoverflow
Solution 3 - IosJason BradyView Answer on Stackoverflow
Solution 4 - IosingcontiView Answer on Stackoverflow
Solution 5 - IosGauravView Answer on Stackoverflow
Solution 6 - IosmultitudesView Answer on Stackoverflow
Solution 7 - IosTomView Answer on Stackoverflow
Solution 8 - IosMubeen QaziView Answer on Stackoverflow