圆角到 UITextField Xcode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30725106/
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-09-15 07:15:26  来源:igfitidea点击:

rounded corner to UITextField Xcode

iosswiftxcodeuser-interfaceuser-experience

提问by PPShein

I have two textfield (username & password) and want to make top rounded corner for username and bottom rounded corner for password as per attachment.

我有两个文本字段(用户名和密码),并希望根据附件为用户名制作顶部圆角,为密码制作底部圆角。

enter image description here

在此处输入图片说明

回答by Ashish Kakkad

Just create a UIView.

只需创建一个UIView.

Put your two text fields in the UIView. Remove the border style of UITextField.

将您的两个文本字段放在UIView. 删除 的边框样式UITextField

yourView.layer.cornerRadius = 10.0
yourView.clipsToBounds = true

回答by user3182143

Just follow the below steps

只需按照以下步骤操作

STEP 1:Import QuartzCore framework in you class:

STEP 1:在你的类中导入 QuartzCore 框架:

      #import <QuartzCore/QuartzCore.h>

STEP 2:Apply the coding

第 2 步:应用编码

      textField.layer.cornerRadius=6.0f;
      textField.layer.masksToBounds=YES;
      textField.layer.borderColor=[[UIColor blueColor]CGColor];
      textField.layer.borderWidth= 1.0f; 

         or

      [textField.layer setBorderColor:[[UIColor blueColor] CGColor]];
      [textField.layer setBorderWidth:2.o];
      [textField.layer setCornerRadius:5.0]; 

回答by Baran Karao?uz

I think you should be write CALayer extension. Swift 3.x - 4.x

我认为您应该编写 CALayer 扩展。斯威夫特 3.x - 4.x

extension CALayer {

    func addRadius(_ corners: UIRectCorner, radius: CGFloat, view: UIView) {
        let mask = CAShapeLayer()
        mask.bounds = view.frame
        mask.position = view.center
        mask.path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
        view.layer.mask = mask
    }

    func addRadius(radius: CGFloat) {
        self.cornerRadius = radius
    }  
}

Use of

用于

    class ViewController: UIViewController {

        @IBOutlet var messageTextField: UITextField!

        override func viewDidLoad() {
            super.viewDidLoad()
            messageTextField.layer.addRadius(radius: 8.0)
        }
    }

Hope your help :)

希望你的帮助:)

回答by MrG

Here is a full code sample:

这是一个完整的代码示例:

import UIKit

导入 UIKit

class RoundedTextField: UITextField {

    override func awakeFromNib() {
        self.layer.cornerRadius = 10.0 //self.frame.size.height/2
        self.clipsToBounds = false

        super.awakeFromNib()
    }
}

回答by s100i29

You can use:

您可以使用:

(UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii

Example:

例子:

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:textField.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path  = maskPath.CGPath;
textField.layer.mask = maskLayer;