0

I need a border to my textfield like the following image.how can i do that?

enter image description here

abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70

2 Answers2

1

try these.. i hope it helps...

UITextField *txt=[[UITextField alloc]initWithFrame:CGRectMake(10, 100, 150, 30)];
[txt setBackgroundColor:[UIColor clearColor]];
[txt setPlaceholder:@"Hello my friend"];
[self.view addSubview:txt];


CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor redColor].CGColor;
border.frame = CGRectMake(0, -2, txt.frame.size.width, txt.frame.size.height);
border.borderWidth = borderWidth;
[txt.layer addSublayer:border];
txt.layer.masksToBounds = YES;
krushnsinh
  • 874
  • 1
  • 10
  • 11
0

I found the answer my own.

just pass the textfield name to the following function

-(void)setBorderView:(UITextField*)textField
{
    UIView *borderView = [[UIView alloc]init];
    borderView.backgroundColor = [UIColor clearColor];
    CGRect frameRectEmail=textField.frame;


    NSLogVariable(textField);
    borderView.layer.borderWidth=1;
    borderView.layer.borderColor=[customColor colorWithHexString:@"8c8b90"].CGColor;
    borderView.layer.cornerRadius=5;
    borderView.layer.masksToBounds=YES;



    UIView *topBorder = [[UIView alloc]init];
    topBorder.backgroundColor = [customColor colorWithHexString:@"1e1a36"];
    CGRect frameRect=textField.frame;
    frameRect.size.height=CGRectGetHeight(textField.frame)/1.5;
    topBorder.frame = frameRect;
      frameRectEmail.size.width=frameRect.size.width;
    [borderView setFrame:frameRectEmail];

    [_textFieldBackGroundView addSubview:borderView];
    [_textFieldBackGroundView addSubview:topBorder];
    [_textFieldBackGroundView addSubview:textField];

}

I am creating a view below the textfield and setting the border to this view.Now i am creating another view with the same baground color and masking the top portion od the borderView.It is not practical in all situations.But for me,it works great.Thanks.

abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70