如何将UITextView的边框设置为与UITextField的边框颜色相同

55
默认情况下,UITextField 的边框颜色为浅灰色。 我想将我的 UITextView 的边框颜色设置成与 UITextField 相同。 我尝试了:
myTextView.layer.borderColor = UIColor.lightGrayColor().CGColor
// or
myTextView.layer.borderColor = UIColor.lightTextColor().CGColor
// or 
myTextView.layer.borderColor = myTextField.layer.borderColor

他们仍然可以拥有不同的颜色。

如何将 UITextView 的边框设置为与 UITextField 的颜色相匹配?


可能是Bordered UITextView的重复问题。 - Sruit A.Suk
我想将边框颜色设置为底部线条。如何做到? - Jayprakash Dubey
8个回答

74

试试这段代码。

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;

调整borderWidth和cornerRadius的值,使其与UITextField的界面完全相同。


2
在Mac上有一个叫做DigitalColor Meter的实用工具。使用该工具获取文本框的精确RGB值,只需将光标拖动到UITextField边框上即可。 - Deepak
6
我使用了0.5的边框宽度,以在iPad mini的屏幕上获得另一个条目的精确外观。 - Caitlin
@caitlin同意,默认的边框宽度不是1。 - lostintranslation

31

这段 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

颜色分量不应该是0.8吗? - redcurry

17

我之前也有类似的问题,是关于iOS 10的,但没有找到答案。不过我使用了实用工具中的数字颜色计和iOS模拟器找到了以下信息。

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

我甚至不知道Mac上有这样的应用程序 :) - bibscy

7

Swift 4.x/iOS 11.

我在模拟器中使用PSD进行了另一项测量。 我可以确认半径为0.5,颜色为0.8,因为205/255=0.8(或者用PSD建议的“cdcdcd”表示为HEX),但是宽度必须小于0.5。 (我附上了一个PSD文件,您可以比较编辑字段(UITExtField)的半径和应用于UITextView的半径。)

所以这是正确的:

    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

注意:我尝试从已经在视图上的TextField中获取颜色,但是我得到了以下结果:
如果边框灰色被定义为self.cellPhoneTxt.layer.borderColor,那么可以使用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)

}

但是在控制台中我得到了以下信息:

kCGColorSpaceModelRGB 0 0 0 1

0.0 0.0 0.0 1.0

因此,苹果似乎使用全黑色和一些透明度。

输入图像描述


4

尝试使用这段Swift代码

let borderColor = UIColor.whiteColor.Cgcolor()

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

这会使它变成白色。 - frezq

4

支持Swift 5.2和深色模式 :)

我只想添加一个使用可适应颜色的语义UI示例。有时候硬编码数值并不是您想要的,特别是如果您支持深色模式。 我认为使用.label作为语义颜色可以使您获得在浅色和深色模式下UITextField边框中使用的默认灰色。 试试这个:

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

我也喜欢用其他语义化颜色进行游戏。这在暗模式下看起来很不错:

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

并且您的UITextView将完全与以下内容相同:

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

我认为这里的问题更多是:“实际的文本字段值是什么?” - RyuX51
你是指RGB值吗?之前已经有一个带有RGB值的答案了,但我想使用语义化颜色。 - multitudes

2

确切的颜色是:

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

0

为了在整个项目中实现通用解决方案,您可以将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));
}

是UIView的扩展。只需将这些UIView+Designable.h/m文件添加到您的项目中,您就可以在属性检查器中看到更多选项。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接