UITextField仅在顶部或底部具有拐角半径

时间:2013-11-11 15:47:16

标签: ios objective-c ios7

我有一个UITextField添加到视图中,我正在尝试更改它的默认外观。

我想要的是改变角半径,但仅限于顶部/底部。我是这样做的:

UIBezierPath *usernameMaskPathWithRadiusTop = [UIBezierPath bezierPathWithRoundedRect:username.bounds
                                                                    byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                                                                          cornerRadii:CGSizeMake(4.0, 4.0)];

UIBezierPath *passwordMaskPathWithRadiusBottom = [UIBezierPath bezierPathWithRoundedRect:password.bounds
                                                                       byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
                                                                             cornerRadii:CGSizeMake(4.0, 4.0)];

CAShapeLayer *usernameMaskLayer = [[CAShapeLayer alloc] init];
usernameMaskLayer.frame = username.bounds;
usernameMaskLayer.path = usernameMaskPathWithRadiusTop.CGPath;

CAShapeLayer *passwordMaskLayer = [[CAShapeLayer alloc] init];
passwordMaskLayer.frame = password.bounds;
passwordMaskLayer.path = passwordMaskPathWithRadiusBottom.CGPath;


[username setTextAlignment:NSTextAlignmentLeft];
[username setClearButtonMode:UITextFieldViewModeWhileEditing];
usernameLayer.shadowOpacity = 0.0;
[usernameLayer setMask:usernameMaskLayer];

[password setTextAlignment:NSTextAlignmentLeft];
[username setClearButtonMode:UITextFieldViewModeWhileEditing];
passwordLayer.shadowOpacity = 0.0;
[passwordLayer setMask:passwordMaskLayer];

UITextField *usernameUITextField *password以及CAShapeLayer *usernameLayer = username.layerCAShapeLayer *passwordLayer = password.layer的定义如上所述。

问题是,在我执行上述操作之后,我根本没有任何字段,实际上它们在那里但是一切都是透明的,文本和背景颜色。我不知道该怎么回来或如何解决我做错的事。

我究竟能做些正确的尝试吗?

1 个答案:

答案 0 :(得分:5)

似乎我必须做的不是设置一个我必须添加为子层的掩码:

[username.layer addSublayer:usernameMaskLayer];

// instead of the old

[usernameLayer setMask:usernameMaskLayer];

然后设置背景我只需setFillColor上的usernameMaskLayer