【问题标题】:How to set UITextField border/CALayer border ios on three sides only如何仅在三个侧面设置 UITextField 边框/CALayer 边框 ios
【发布时间】:2015-08-03 06:12:55
【问题描述】:

我的文本字段需要一个边框,如下图所示。我该怎么做?

【问题讨论】:

标签: ios objective-c border calayer


【解决方案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;

【讨论】:

  • 我明白了你的逻辑,它应该可以工作。但它不是。不知道为什么
【解决方案2】:

我自己找到了答案。

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

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

}

我正在文本字段下方创建一个视图并将边框设置为此视图。现在我正在创建另一个具有相同背景颜色的视图并掩盖边框视图的顶部。这在所有情况下都不实用。但对我来说,效果很好。谢谢。

【讨论】:

    猜你喜欢
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 2011-08-12
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    相关资源
    最近更新 更多