码迷,mamicode.com
首页 > 其他好文 > 详细

CoreGraphics 自定义button

时间:2016-05-05 21:56:24      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

    //去的画布的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //画布大小
    CGContextAddRect(context, rect);
    //画布颜色
    [self.buttonColor set];
    //填充
    CGContextFillPath(context);

    UIBezierPath *roundeRectanglePath = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, 1, 1) cornerRadius:rect.size.height/2];
    //填充颜色
    [self.buttonColor setFill];
    //连线
    [roundeRectanglePath fill];
    //线颜色
    [[UIColor whiteColor] setStroke];
    //线宽度
    roundeRectanglePath.lineWidth = 1;

    [roundeRectanglePath stroke];
    
    //
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSDictionary *att = @{NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:14]};
    CGSize size = [self.buttonTitle sizeWithAttributes:att];
    CGRect r = CGRectMake(rect.size.width/2 - size.width/2, rect.size.height/2 - size.height/2, size.width, size.height);
    //可以是image,这样就可以绘制图像
    [self.buttonTitle drawInRect:r withAttributes:att];

 点击效果

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    NSUInteger tapCount = touch.tapCount;
    switch (tapCount) {
        case 1:
            self.buttonClickBlock();
            break;
    
        default:
            break;
    }
}

 

最终效果

技术分享

 

 

.h文件

 

CoreGraphics 自定义button

标签:

原文地址:http://www.cnblogs.com/huoxingdeguoguo/p/5462676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!