2016-11-07 86 views
1

This is textfield which I get right now如何让某些角落的文本框变圆?

我用下面的代码:

- (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]; 
} 

这个问题是圆角得到切断。

+0

你为什么不使用layer.corner.raius来去做? – SeanChense

+0

你从哪里调用这个方法? – alexburtnik

+0

@alexburtnik - (void)viewDidLayoutSubviews { [self initUIfeatures]; } - (无效)initUIfeatures { [authTextField setMaskByRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft)withCornerRadius:8.0f]; } – Viktorianec

回答

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

+0

不错的工作人[再]! – Viktorianec

0

我为此做了自定义类。你可以使用下面的代码,任何可以改变的故事板,只要你想和角落的半径你想要的。

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() 
    } 

}