如何仅在三面设置UITextField边框/ CALayer边框ios

时间:2015-08-03 06:12:55

标签: ios objective-c border calayer

我需要一个边框到我的文本域,如下图所示。我可以这样做吗?

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试这些.. 我希望它有所帮助...

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;

答案 1 :(得分:0)

我找到了自己的答案。

只需将文本域名称传递给以下函数

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

}

我正在文本字段下方创建一个视图,并将边框设置为此视图。现在我正在创建另一个具有相同baground颜色的视图并屏蔽borderView的顶部部分。它在所有情况下都不实用。但对我来说,它很棒。谢谢。