ios 如何将 UITextView 的边框设置为与 UITextField 的边框颜色相同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25962449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the border of UITextView to same as border color of UITextField
提问by n179911
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.
默认情况下 UITextField 的边框颜色为浅灰色。我想将 UITextView 设置为与 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. Can you please tell me how to see teh UITextView border to match UITextField color?
他们还得有不同的颜色。你能告诉我如何查看 UITextView 边框以匹配 UITextField 颜色吗?
Thank you.
谢谢你。
回答by Deepak
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.
更改 borderWidth 和 cornerRadius 值以获取与 UITextField 相同的 ui。
回答by Aks
This Swift code also works for setting same border color, width & radius as UITextField
:
此 Swift 代码也适用于设置与以下相同的边框颜色、宽度和半径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
回答by Jason Brady
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.
我在 iOS 10 上遇到了类似的问题,但无法找到答案,但这是我使用实用工具和 iOS 模拟器中的数字色度计发现的问题。
Swift 3 / iOS 10
斯威夫特 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
回答by ingconti
swift 4.x/ios11.
快速 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.
我使用模拟器在 PSD 中做了另一个测量。我可以确认半径为 0.5,颜色为 0.8,因为 205/255 = 0.8(或十六进制中的“cdcdcd”,正如 PSD 所建议的,但宽度必须为0.5。(我附上了一个 PSD,您可以在其中比较编辑字段的半径(UITextField) ) AND 半径应用于 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:
注意:我试图从已经在 View 上的 TextField 获取颜色,但我得到了:
if let borderGray = self.cellPhoneTxt.layer.borderColor{ let BG = UIColor(cgColor: borderGray)
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
kCGColorSpaceModelRGB 0 0 0 1
0.0 0.0 0.0 1.0
0.0 0.0 0.0 1.0
so it seems Apple is using full black AND some Alpha.
所以看起来Apple正在使用全黑和一些Alpha。
回答by Gaurav
Try this code for Swift
试试这个 Swift 代码
let borderColor = UIColor.whiteColor.Cgcolor()
myTextView.layer.borderColor = borderColor.CGColor;
myTextView.layer.borderWidth = 1.0;
myTextView.layer.cornerRadius = 5.0;
回答by Tom
The exact color is:
准确的颜色是:
Objective-C:
目标-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
回答by Mubeen Qazi
For generic solution through out the project. You can extern UIView class and add these methods to it.
对于整个项目的通用解决方案。您可以外部 UIView 类并将这些方法添加到它。
-(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));
}
Itsan 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.
它是 UIView 的扩展。只需在您的项目中添加这些 UIView+Designable.h/m 文件,您就可以在属性检查器中看到更多选项。