ios UITextField - 圆角问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13717007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:25:25  来源:igfitidea点击:

UITextField - Rounded corner issue

iphoneobjective-ciosios5uitextfield

提问by iOS

I bring the issue forward which I face. I am creating a UITextFieldprogrammatically as below.

我提出了我面临的问题。我正在以UITextField编程方式创建如下。

UItextField *mobileNumberField = [[UITextField alloc] initWithFrame:CGRectMake(10, 195, 300, 41)];
mobileNumberField.delegate = self;
mobileNumberField.borderStyle = UITextBorderStyleRoundedRect;
[mobileNumberField.layer setCornerRadius:14.0f];
mobileNumberField.placeholder = @"Mobile Number";
[self.paymentsHomeView addSubview:mobileNumberField];

The output is the attached image.

输出是附加的图像。

enter image description here

在此处输入图片说明

I dont know why is it breaking at the corners. Help me to fix my text field like the image attached below.

我不知道为什么它会在角落处破裂。帮助我修复我的文本字段,如下图所示。

enter image description here

在此处输入图片说明

Thanks in advance..!

提前致谢..!

回答by Paras Joshi

just remove this line...

只需删除此行...

mobileNumberField.borderStyle = UITextBorderStyleRoundedRect;

and add this code also..

并添加此代码..

    [mobileNumberField setBackgroundColor:[UIColor whiteColor]];
    [mobileNumberField.layer setBorderColor:[UIColor grayColor].CGColor];
    [mobileNumberField.layer setBorderWidth:1.0];

i hope this help you..

我希望这对你有帮助..

回答by Paramasivan Samuttiram

Update your like below.

在下面更新您的喜欢。

    UITextField *mobileNumberField = [[UITextField alloc] initWithFrame:CGRectMake(10, 195, 300, 41)];
    mobileNumberField.delegate = self;
    mobileNumberField.layer.borderWidth = 1.0f;
    mobileNumberField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    mobileNumberField.
//    mobileNumberField.borderStyle = UITextBorderStyleRoundedRect;
    [mobileNumberField.layer setCornerRadius:14.0f];
    mobileNumberField.placeholder = @"Mobile Number";
    [self.paymentsHomeView addSubview:mobileNumberField];

回答by Miti

textField.layer.cornerRadius=textfield.frame.size.height/2;

textField.clipsToBounds=YES;

回答by oyalhi

The reason the corners are cut is because there is an enclosing view to the text field. When you set the corner radius, applies to THAT view and thus the corners of the inside text field seem to be cut - in reality they have not even changed.

切角的原因是因为文本字段有一个封闭的视图。当您设置角半径时,适用于该视图,因此内部文本字段的角似乎被切割 - 实际上它们甚至没有改变。

The solution is to put the UITextFieldinside UIView, set textfield borderstyle to none. Then apply the border and corner radius specification to the uiview. Note the borderColor, which is very close, if not the same, to the UITextField borderColor.

解决办法是把UITextField里面UIView,设置textfield borderstyle 为none。然后将边框和角半径规范应用于 uiview。注意 borderColor,它与 UITextField borderColor 非常接近,如果不一样的话。

As of writing, tested and works in Xcode 7.3.1, Swift 2.2, iOS 8 and 9.

在编写、测试和在 Xcode 7.3.1、Swift 2.2、iOS 8 和 9 中工作。

Swift:

迅速:

textField.borderStyle = UITextBorderStyle.None
textBorderView.layer.cornerRadius = 5
textBorderView.layer.borderWidth = 1
textBorderView.layer.borderColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.2).CGColor

回答by junaidsidhu

Here is the solution of your problem

这是您问题的解决方案

UITextField * txtField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];

[txtField setBorderStyle:UITextBorderStyleNone];

[txtField.layer setMasksToBounds:YES];
[txtField.layer setCornerRadius:10.0f];
[txtField.layer setBorderColor:[[UIColor lightGrayColor]CGColor]];
[txtField.layer setBorderWidth:1];
[txtField setTextAlignment:UITextAlignmentCenter];
[txtField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self.view addSubview:txtField];

回答by Prasad09

Swift 3 solution:

斯威夫特 3 解决方案:

I have written separate function to set border and corner radius to any layer in swift, you have to just pass the layer of any view, border width, corner radius and border color to the following function

我已经编写了单独的函数来在 swift 中为任何图层设置边框和角半径,您只需将任何视图的图层、边框宽度、角半径和边框颜色传递给以下函数

` func setBorderAndCornerRadius(layer: CALayer, width: CGFloat, radius: CGFloat,color : UIColor ) {
        layer.borderColor = color.cgColor
        layer.borderWidth = width
        layer.cornerRadius = radius
        layer.masksToBounds = true
    }

`

`

回答by cnu

swift solution:

快速解决方案:

textField.layer.borderWidth = anyWidth
textField.layer.borderColor = anyColor
textField.layer.cornerRadius = textField.frame.size.height/2
textField.clipsToBounds = true