扫码关注官方订阅号
一个textfield只在上半部分加圆角怎么做
小伙看你根骨奇佳,潜力无限,来学PHP伐。
var rect = CGRect(x: 0, y: 0, width: 200, height: 40) var textfield = UITextField(frame: rect) textfield.backgroundColor = UIColor.lightGrayColor() // 背景颜色 textfield.text = "GAGA" textfield.textAlignment = NSTextAlignment.Center textfield.textColor = UIColor.darkGrayColor() var maskPath = UIBezierPath(roundedRect: textfield.bounds, byRoundingCorners: UIRectCorner.TopLeft | UIRectCorner.TopRight, // 左上右上圆角 cornerRadii: CGSize(width: 12, height: 12)) // 圆角半径 /* 边框 */ var borderLayer = CAShapeLayer() borderLayer.frame = textfield.bounds borderLayer.path = maskPath.CGPath borderLayer.strokeColor = UIColor.darkGrayColor().CGColor // 边框颜色 borderLayer.fillColor = UIColor.clearColor().CGColor /* 遮罩 */ var maskLayer = CAShapeLayer() maskLayer.frame = textfield.bounds maskLayer.path = maskPath.CGPath textfield.layer.mask = maskLayer textfield.layer.addSublayer(borderLayer)
UIBezierPath
CAShapeLayer
参考:http://stackoverflow.com/questions/2264083/rounded-uiview-using-calaye...
http://segmentfault.com/q/1010000002548754 不如做张图最简单
写一个uitextfield的分类,主要代码参考如下:
参考
- (void)applyRoundCorners:(UIRectCorner)corners radius:(CGFloat)radius { UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } //再调用即可 [self.yourtextfield applyRoundCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:5];
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
UIBezierPath画出你想要的部分圆角路径。CAShapeLayer作为 textfield 的遮罩,即可得到圆角。CAShapeLayer,设置透明背景色、以及合适的线条颜色和线条粗细,作为边框。加入到 textfield 中作为子 layer。参考:http://stackoverflow.com/questions/2264083/rounded-uiview-using-calaye...
http://segmentfault.com/q/1010000002548754
不如做张图最简单
写一个uitextfield的分类,主要代码
参考如下: