UITextField仅在顶部或底部具有拐角半径 [英] UITextField with corner radius only on top or bottom

查看:83
本文介绍了UITextField仅在顶部或底部具有拐角半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITextField that is added to a view and I'm trying to change it's default appearance.

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

What I want is to change the corner radius, but only on top / bottom. I'm doing so by doing this:

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 *用户名 UITextField *密码 CAShapeLayer * usernameLayer = username.layer CAShapeLayer * passwordLayer = password.layer 按照描述定义。

Where UITextField *username, UITextField *password and the CAShapeLayer *usernameLayer = username.layer and CAShapeLayer *passwordLayer = password.layer are defined as described.

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

The issue is that after I perform that above, I get no fields at all, actually they are there but everything is transparent, text and background color. I'm not sure how to get that back or how to fix whatever I'm doing wrong.

我怎样才能做我正在做的事情?

How exactly can I do what I'm trying right ?

推荐答案

看起来我不得不设置一个我必须添加为子层的掩码:

It seems like what I had to do is instead of setting a mask I had to add as sublayer:

[username.layer addSublayer:usernameMaskLayer];

// instead of the old

[usernameLayer setMask:usernameMaskLayer];

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

And then for setting a background I just had to setFillColor on the usernameMaskLayer.

这篇关于UITextField仅在顶部或底部具有拐角半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆