UITextField边框颜色

das*_*sha 127 border colors uitextfield uikit ios

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

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

self.textField.backgroundColor = textFieldColor;
Run Code Online (Sandbox Code Playgroud)

但我也必须改变UITextField边框的颜色.我的问题是如何改变边框颜色.

Gar*_*ary 271

QuartzCore在您的类中导入框架:

#import <QuartzCore/QuartzCore.h>
Run Code Online (Sandbox Code Playgroud)

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

    textField.layer.cornerRadius=8.0f;
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor=[[UIColor redColor]CGColor];
    textField.layer.borderWidth= 1.0f;
Run Code Online (Sandbox Code Playgroud)

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

    serverField.layer.borderColor=[[UIColor clearColor]CGColor];
Run Code Online (Sandbox Code Playgroud)

在快速的代码中

    textField.layer.borderWidth = 1
    textField.layer.borderColor = UIColor.whiteColor().CGColor
Run Code Online (Sandbox Code Playgroud)

  • 在iOS 7中,您必须设置边框宽度或颜色不起作用. (53认同)
  • 它是#import <QuartzCore/QuartzCore.h>而不是QuartCore(你忘记了z) (3认同)

iOS*_*wan 20

试试这个:

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];
Run Code Online (Sandbox Code Playgroud)

并导入QuartzCore:

#import <QuartzCore/QuartzCore.h>
Run Code Online (Sandbox Code Playgroud)


jam*_*obo 17

导入以下类:

#import <QuartzCore/QuartzCore.h> 
Run Code Online (Sandbox Code Playgroud)

//用于设置文本字段边框的灰色的代码

[[textField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0
                                                   green:171.0/255.0
                                                    blue:171.0/255.0
                                                   alpha:1.0] CGColor]];
Run Code Online (Sandbox Code Playgroud)

171.0根据需要更换相应的颜色编号.


Jov*_*in_ 13

这个问题在Google搜索中显示得相当高,并且大部分都在使用!我确实发现Salman Zaidi的答案对iOS 7来说是部分正确的.

您需要修改"还原"代码.我发现以下用于还原工作完美:

textField.layer.cornerRadius = 0.0f;
textField.layer.masksToBounds = YES;
textField.layer.borderColor = [[UIColor blackColor] CGColor];
textField.layer.borderWidth = 0.0f;
Run Code Online (Sandbox Code Playgroud)

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


sky*_*der 10

为了简化从接受的答案这个动作,你也可以创建类别UIView(因为这种方式更适合的UIView的子类,不仅为文本框:

UIView的+ Additions.h:

#import <Foundation/Foundation.h>

@interface UIView (Additions)
- (void)setBorderForColor:(UIColor *)color 
                    width:(float)width 
                   radius:(float)radius;
@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

用法:

#import "UIView+Additions.h"
//...
[textField setBorderForColor:[UIColor redColor]
                       width:1.0f
                      radius:8.0f];
Run Code Online (Sandbox Code Playgroud)


小智 8

如果您使用带圆角的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;
Run Code Online (Sandbox Code Playgroud)

要删除边框:

self.TextField.layer.masksToBounds=NO;
self.TextField.layer.borderColor=[[UIColor clearColor]CGColor];
Run Code Online (Sandbox Code Playgroud)


小智 6

迅速更新5.0

textField.layer.masksToBounds = true
textField.layer.borderColor = UIColor.blue.cgColor
textField.layer.borderWidth = 1.0
Run Code Online (Sandbox Code Playgroud)