【问题标题】:How get textfield with some corners rounded?如何获得一些圆角的文本字段?
【发布时间】:2016-11-07 09:48:03
【问题描述】:

我使用下一个代码:

- (void)setMaskByRoundingCorners:(UIRectCorner)corners withCornerRadius:(float)radius
    {
        UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];

        CAShapeLayer* shape = [[CAShapeLayer alloc] init];
        [shape setPath:rounded.CGPath];
        shape.frame = self.bounds;
        self.layer.mask = shape;
    }

但现在我看到了这种奇怪的效果。

我从 viewcontroller 调用它,在 didlayoutsubviews 之后。我这样做是为了更新主线程。

- (void)viewDidLayoutSubviews
{
    [self initUIfeatures];
}

- (void)initUIfeatures
{
    [authTextField setMaskByRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft) withCornerRadius:8.0f]; 
}

问题是圆角被截断。

【问题讨论】:

  • 你为什么不使用 layer.corner.raius 来做呢?
  • 你从哪里调用这个方法?
  • @alexburtnik - (void)viewDidLayoutSubviews { [self initUIfeatures]; } - (void)initUIfeatures { [authTextField setMaskByRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft) withCornerRadius:8.0f]; }
  • @ZaidPathan 它不是重复的!我的问题是文本字段而不是简单的 UIView。我将我的函数用于 uiview 的所有其他孩子,它工作正常,但这里我有一个错误。
  • @Viktorianec,给定的源代码有效吗?

标签: ios interface textfield quartz-core


【解决方案1】:

添加这些功能,

-(void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius
{
    CGRect bounds = _IBtxtField.bounds;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
                                                   byRoundingCorners:corners
                                                         cornerRadii:CGSizeMake(radius, radius)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = bounds;
    maskLayer.path = maskPath.CGPath;

    _IBtxtField.layer.mask = maskLayer;

    CAShapeLayer*   frameLayer = [CAShapeLayer layer];
    frameLayer.frame = bounds;
    frameLayer.path = maskPath.CGPath;
    frameLayer.strokeColor = [UIColor blackColor].CGColor;
    frameLayer.fillColor = nil;

    [_IBtxtField.layer addSublayer:frameLayer];
}

-(void)roundCornersRadius:(CGFloat)radius
{
    [self roundCorners:(UIRectCornerTopLeft|UIRectCornerTopRight | UIRectCornerBottomLeft) radius:radius];
}

这样使用,

[self roundCornersRadius:10];

Ref

SourceCode

【讨论】:

    【解决方案2】:

    我已经为相同的自定义类。您可以使用以下代码在情节提要中随心所欲地更改任何您想要的圆角半径。

    import UIKit
    
    @IBDesignable
    class CustomRoundedTextField: UITextField {
    
        @IBInspectable var lColor: UIColor = UIColor(red: (37.0/255.0), green: (252.0/255), blue: (244.0/255.0), alpha: 1.0)
        @IBInspectable var lWidth: CGFloat = 1
        @IBInspectable var lCornerRadius: CGFloat = 8
        @IBInspectable var sColor:UIColor = UIColor(red: (37.0/255.0), green: (252.0/255), blue: (244.0/255.0), alpha: 1.0)
        @IBInspectable var TLRCorner:Bool = false
        @IBInspectable var TRRCorner:Bool = false
        @IBInspectable var BLRCorner:Bool = false
        @IBInspectable var BRRCorner:Bool = false
        @IBInspectable var XInset:CGFloat = 10
        @IBInspectable var YInset:CGFloat = 10
    
        required internal init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
        }
    
        override internal func drawRect(rect: CGRect) {
            addBorderFieldRect()
        }
    
        override func textRectForBounds(bounds: CGRect) -> CGRect {
            return CGRectInset(bounds, XInset, 0)
        }
    
        override func editingRectForBounds(bounds: CGRect) -> CGRect {
            return CGRectInset(bounds, YInset, 0)
        }
    
        func addBorderFieldRect() {
            let rectanglePath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [
                TLRCorner ? .TopLeft : [],
                TRRCorner ? .TopRight : [],
                BLRCorner ? .BottomLeft : [],
                BRRCorner ? .BottomRight : []
                ], cornerRadii: CGSizeMake(lCornerRadius, lCornerRadius))
            rectanglePath.closePath()
            self.lColor.setFill()
            rectanglePath.fill()
            self.sColor.setStroke()
            rectanglePath.lineWidth = lWidth
            rectanglePath.stroke()
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多