2015年9月2日 星期三

選擇性圓角的 UITextField


最近想試試如何把 UITableView 的 Grouping 設計應用到連續幾個的 UITextField。研究了一會,編寫了以下程序:
- (void)roundedCorners:(UIRectCorner)corners radius:(CGFloat)radius view:(UIView *)view  {
    CGRect bounds = view.bounds;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];

    //  Set rounded corner mask
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    [maskLayer setFrame:bounds];
    [maskLayer setPath:maskPath.CGPath];
    [view.layer setMask:maskLayer];
}

如果想有邊框的話,可以這樣:
- (void)roundedCorners:(UIRectCorner)corners radius:(CGFloat)radius view:(UIView *)view  {
    CGRect bounds = view.bounds;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];

    //  Set rounded corner mask
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    [maskLayer setFrame:bounds];
    [maskLayer setPath:maskPath.CGPath];
    [view.layer setMask:maskLayer];

    //  Set rounded corner border
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    [borderLayer setFrame:bounds];
    [borderLayer setPath:maskPath.CGPath];
    [borderLayer setStrokeColor:[UIColor grayColor].CGColor];
    [borderLayer setFillColor:nil];
    [borderLayer setLineWidth:3.0f];
    [view.layer addSublayer:borderLayer];
}

沒有留言: