【发布时间】:2015-08-03 06:12:55
【问题描述】:
【问题讨论】:
-
使用图片作为背景,不要给uitextfield设置边框...
标签: ios objective-c border calayer
【问题讨论】:
标签: ios objective-c border calayer
试试这些.. 希望对你有帮助...
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;
【讨论】:
我自己找到了答案。
只需将文本字段名称传递给以下函数
-(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];
}
我正在文本字段下方创建一个视图并将边框设置为此视图。现在我正在创建另一个具有相同背景颜色的视图并掩盖边框视图的顶部。这在所有情况下都不实用。但对我来说,效果很好。谢谢。
【讨论】: