ios UITextField 边框颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1861527/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:41:32  来源:igfitidea点击:

UITextField border color

ioscolorsuikituitextfieldborder

提问by dasha

I have really great wish to set my own color to UITextField border. But so far I could find out how to change the border line style only.

我非常希望将自己的颜色设置为 UITextField 边框。但到目前为止,我只能找到如何更改边框线样式。

I've used background property to set background color in such way:

我已经使用背景属性以这种方式设置背景颜色:

self.textField.backgroundColor = textFieldColor;

But I have to change color of UITextField border too. And my question was about how to change the border color.

但是我也必须更改 UITextField 边框的颜色。我的问题是关于如何更改边框颜色。

回答by Gary

Import QuartzCoreframework in you class:

QuartzCore在您的课程中导入框架:

#import <QuartzCore/QuartzCore.h>

and for changing the border color use the following code snippet (I'm setting it to redColor),

并且要更改边框颜色,请使用以下代码片段(我将其设置为 redColor),

    textField.layer.cornerRadius=8.0f;
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor=[[UIColor redColor]CGColor];
    textField.layer.borderWidth= 1.0f;

For reverting back to the original layout just set border color to clear color,

要恢复到原始布局,只需将边框颜色设置为清除颜色,

    serverField.layer.borderColor=[[UIColor clearColor]CGColor];

in swift code

在快速代码中

    textField.layer.borderWidth = 1
    textField.layer.borderColor = UIColor.whiteColor().CGColor

回答by iOSPawan

Try this:

尝试这个:

UITextField *theTextFiels=[[UITextField alloc]initWithFrame:CGRectMake(40, 40, 150, 30)];
    theTextFiels.borderStyle=UITextBorderStyleNone;
    theTextFiels.layer.cornerRadius=8.0f;
    theTextFiels.layer.masksToBounds=YES;
        theTextFiels.backgroundColor=[UIColor redColor];
    theTextFiels.layer.borderColor=[[UIColor blackColor]CGColor];
    theTextFiels.layer.borderWidth= 1.0f;

    [self.view addSubview:theTextFiels];
    [theTextFiels release];

and import QuartzCore:

并导入 QuartzCore:

#import <QuartzCore/QuartzCore.h>

回答by james lobo

Import the following class:

导入以下类:

#import <QuartzCore/QuartzCore.h> 

//Code for setting the grey color for the border of the text field

//设置文本框边框灰色的代码

[[textField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0
                                                   green:171.0/255.0
                                                    blue:171.0/255.0
                                                   alpha:1.0] CGColor]];

Replace 171.0with the respective color number as required.

171.0根据需要替换为相应的颜色编号。

回答by Jovin_

this question shows up pretty high on a Google search and worked for the most part! I did find that Salman Zaidi's answer was partially correct for iOS 7.

这个问题在谷歌搜索中出现得相当高,而且大部分都有效!我确实发现 Salman Zaidi 的回答对 iOS 7 部分正确。

You need to make a modification to the "reverting" code. I found that the following for reverting worked perfectly:

您需要对“恢复”代码进行修改。我发现以下用于恢复的工作完美无缺:

textField.layer.cornerRadius = 0.0f;
textField.layer.masksToBounds = YES;
textField.layer.borderColor = [[UIColor blackColor] CGColor];
textField.layer.borderWidth = 0.0f;

I understand that this is most likely due to changes in iOS 7.

我知道这很可能是由于 iOS 7 的变化。

回答by skywinder

To simplify this actions from accepted answer, you can also create Categoryfor UIView(since this works for all subclasses of UIView, not only for textfields:

为了从接受的答案中简化此操作,您还可以创建Categoryfor UIView(因为这适用于 UIView 的所有子类,不仅适用于文本字段:

UIView+Additions.h:

UIView+Additions.h:

#import <Foundation/Foundation.h>

@interface UIView (Additions)
- (void)setBorderForColor:(UIColor *)color 
                    width:(float)width 
                   radius:(float)radius;
@end

UIView+Additions.m:

UIView+Additions.m:

#import "UIView+Additions.h"

@implementation UIView (Additions)

- (void)setBorderForColor:(UIColor *)color 
                    width:(float)width
                   radius:(float)radius
{
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
    self.layer.borderColor = [color CGColor];
    self.layer.borderWidth = width;
}

@end

Usage:

用法:

#import "UIView+Additions.h"
//...
[textField setBorderForColor:[UIColor redColor]
                       width:1.0f
                      radius:8.0f];

回答by Stefan Liesendahl

If you use a TextField with rounded corners use this code:

如果您使用带圆角的 TextField,请使用以下代码:

    self.TextField.layer.cornerRadius=8.0f;
    self.TextField.layer.masksToBounds=YES;
    self.TextField.layer.borderColor=[[UIColor redColor]CGColor];
    self.TextField.layer.borderWidth= 1.0f;

To remove the border:

要删除边框:

self.TextField.layer.masksToBounds=NO;
self.TextField.layer.borderColor=[[UIColor clearColor]CGColor];

回答by Hugo Jordao

Update for swift 5.0

Swift 5.0 的更新

textField.layer.masksToBounds = true
textField.layer.borderColor = UIColor.blue.cgColor
textField.layer.borderWidth = 1.0

回答by Rameswar Prasad

borderColoron any view(or UIView Subclass) could also be set using storyboard with a little bit of coding and this approach could be really handy if you're setting border color on multiple UI Objects.

任何视图(或 UIView 子类)上的borderColor也可以使用情节提要和一点点编码来设置,如果您在多个 UI 对象上设置边框颜色,这种方法可能非常方便。

Below are the steps how to achieve it,

以下是如何实现它的步骤,

  1. Create a category on CALayer class. Declare a property of type UIColorwith a suitable name, I'll name it as borderUIColor.
  2. Write the setter and getter for this property.
  3. In the 'Setter' method just set the "borderColor" property of layer to the new colors CGColor value.
  4. In the 'Getter' method return UIColor with layer's borderColor.
  1. 在 CALayer 类上创建一个类别。使用合适的名称声明UIColor类型的属性,我将其命名为borderUIColor
  2. 为这个属性编写 setter 和 getter。
  3. 在“Setter”方法中,只需将图层的“borderColor”属性设置为新颜色的 CGColor 值。
  4. 在'Getter' 方法中返回带有图层边框颜色的 UIColor。

P.S: Remember, Categories can't have stored properties. 'borderUIColor' is used as a calculated property, just as a reference to achieve what we're focusing on.

PS:请记住,类别不能具有存储属性。'borderUIColor' 用作计算属性,作为实现我们关注的内容的参考。

Please have a look at the below code sample;

请看下面的代码示例;

Objective C:

目标 C:

Interface File:

接口文件:

#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>

@interface CALayer (BorderProperties)

// This assigns a CGColor to borderColor.
@property (nonatomic, assign) UIColor* borderUIColor;

@end

Implementation File:

实施文件:

#import "CALayer+BorderProperties.h"

@implementation CALayer (BorderProperties)

- (void)setBorderUIColor:(UIColor *)color {
    self.borderColor = color.CGColor;
}

- (UIColor *)borderUIColor {
    return [UIColor colorWithCGColor:self.borderColor];
}

@end

Swift 2.0:

斯威夫特 2.0:

extension CALayer {
var borderUIColor: UIColor {
    set {
        self.borderColor = newValue.CGColor
    }

    get {
        return UIColor(CGColor: self.borderColor!)
    }
}
}

And finally go to your storyboard/XIB, follow the remaining steps;

最后转到您的故事板/XIB,按照其余步骤操作;

  1. Click on the View object for which you want to set border Color.
  2. Click on "Identity Inspector"(3rd from Left) in "Utility"(Right side of the screen) panel.
  3. Under "User Defined Runtime Attributes", click on the "+" button to add a key path.
  4. Set the type of the key path to "Color".
  5. Enter the value for key path as "layer.borderUIColor". [Remember this should be the variable nameyou declared in category, not borderColorhere it's borderUIColor].
  6. Finally chose whatever color you want.
  1. 单击要为其设置边框颜色的视图对象。
  2. 单击“Utility”(屏幕右侧)面板中的“Identity Inspector”(左起第三个)。
  3. 在“用户定义的运行时属性”下,单击“+”按钮以添加关键路径。
  4. 将键路径的类型设置为“颜色”。
  5. 输入键路径的值作为“layer.borderUIColor”。[请记住,这应该是您在类别中声明的变量名称,而不是borderColor,这里是borderUIColor]。
  6. 最后选择你想要的任何颜色。

You've to set layer.borderWidthproperty value to at least 1 to see the border color.

您必须将layer.borderWidth属性值设置为至少 1 才能看到边框颜色。

Build and Run. Happy Coding. :)

构建和运行。快乐编码。:)

回答by meow2x

Here's a Swift implementation. You can make an extension so that it will be usable by other views if you like.

这是一个 Swift 实现。如果您愿意,您可以进行扩展,以便其他视图可以使用它。

extension UIView {
    func addBorderAndColor(color: UIColor, width: CGFloat, corner_radius: CGFloat = 0, clipsToBounds: Bool = false) {
        self.layer.borderWidth  = width
        self.layer.borderColor  = color.cgColor
        self.layer.cornerRadius = corner_radius
        self.clipsToBounds      = clipsToBounds
    }
}

Call this like: email.addBorderAndColor(color: UIColor.white, width: 0.5, corner_radius: 5, clipsToBounds: true).

像这样调用: email.addBorderAndColor(color: UIColor.white, width: 0.5, corner_radius: 5, clipsToBounds: true)