iOS绘制view

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

    UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:[self cornerRadius]];

    [roundedRect addClip];

    [[UIColor whiteColor] setFill];

    UIRectFill(self.bounds);

    

    [[UIColor blackColor] setStroke];

    [roundedRect stroke];

    

    if(self.faceUP){

        UIImage *faceImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@", [self rankAsString], self.suit]];

        if (faceImage) {

            CGRect imageRect = CGRectInset(self.bounds,

                                           self.bounds.size.width * (1.0-self.faceCardScaleFactor),

                                           self.bounds.size.height * (1.0-self.faceCardScaleFactor));

            [faceImage drawInRect:imageRect];

        } else {

            [self drawPips];

        }

        [self drawCorners];

    }else{

        [[UIImage imageNamed:@"cardback"] drawInRect:self.bounds];

    }

    NSLog(@"drawRect");

} 

这里设置了倒角,阴影,颜色,还有图片 

 

- (void)pushContextAndRotateUpsideDown

{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    CGContextTranslateCTM(context, self.bounds.size.width, self.bounds.size.height);

    CGContextRotateCTM(context, M_PI);

}


- (void)popContext

{

    CGContextRestoreGState(UIGraphicsGetCurrentContext());

}


#pragma mark - Corners

- (void)drawCorners

{

    NSMutableParagraphStyle *paragraphyStyle = [[NSMutableParagraphStyle alloc] init];

    paragraphyStyle.alignment = NSTextAlignmentCenter;

    

    UIFont *cornerFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];

    cornerFont = [cornerFont fontWithSize:cornerFont.pointSize * [self cornerScaleFactor]];

    

    NSAttributedString *cornerText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@", [self rankAsString], self.suit] attributes:@{ NSFontAttributeName : cornerFont, NSParagraphStyleAttributeName : paragraphyStyle}];

    

    CGRect textBounds;

    textBounds.origin = CGPointMake([self cornerOffset], [self cornerOffset]);

    textBounds.size = [cornerText size];

    [cornerText drawInRect:textBounds];

    

    [self pushContextAndRotateUpsideDown];

    [cornerText drawInRect:textBounds];

    [self popContext];

}

这里设置了角落上的suit和rank 

posted @ 2015-03-09 19:12  罕见  阅读(408)  评论(0编辑  收藏  举报