UITextField 테두리 색 같은 방법으로 배경색을 설정했습니다. self.textField.backgroundColor = textFieldColor; 그러나 UITextField

내 자신의 색상을 UITextField 테두리로 설정하고 싶습니다. 그러나 지금까지 경계선 스타일 만 변경하는 방법을 알 수있었습니다.

background 속성을 사용하여 다음과 같은 방법으로 배경색을 설정했습니다.

self.textField.backgroundColor = textFieldColor;

그러나 UITextField 테두리의 색상도 변경해야합니다. 그리고 내 질문은 테두리 색상을 변경하는 방법에 관한 것입니다.



답변

QuartzCore수업 중 프레임 워크 가져 오기 :

#import <QuartzCore/QuartzCore.h>

테두리 색상을 변경하려면 다음 코드 스 니펫을 사용하십시오 (빨간색으로 설정합니다).

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

원래 레이아웃으로 되돌리려면 테두리 색상을 선명한 색상으로 설정하십시오.

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

신속한 코드로

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


답변

이 시도:

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];

QuartzCore를 가져옵니다.

#import <QuartzCore/QuartzCore.h>


답변

다음 클래스를 가져옵니다.

#import <QuartzCore/QuartzCore.h> 

// 텍스트 필드의 테두리에 회색을 설정하는 코드

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

171.0필요에 따라 해당 색상 번호로 교체하십시오 .


답변

이 질문은 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;

iOS 7의 변경으로 인한 것 같습니다.


답변

허용 된 답변 에서이 작업을 단순화하기 위해 범주 를 만들 수도 있습니다 UIView(텍스트 필드뿐만 아니라 UIView의 모든 하위 클래스에서 작동하기 때문에).

UIView + Additions.h :

#import <Foundation/Foundation.h>

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

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

용법:

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


답변

모서리가 둥근 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;

테두리를 제거하려면

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


답변

swift 5.0 업데이트

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