1、字体设置
#define CORNER_FON_STANDARD_HEIGHT 180.0 -(CGFloat)cornerScaleFator { return self.bounds.size.height / CORNER_FON_STANDARD_HEIGHT;} UIFont *cornerFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; cornerFont = [cornerFont fontWithSize:cornerFont.pointSize * [self cornerScaleFator]];
2、段落设置
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;
3、绘图
1 - (void)drawRect:(CGRect)rect 2 { 3 // Drawing code 4 UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:self.bounds 5 cornerRadius:[self cornerRadius]]; 6 [roundedRect addClip]; 7 8 [[UIColor whiteColor] setFill]; 9 UIRectFill(self.bounds); 10 11 [[UIColor blackColor] setStroke]; 12 [roundedRect stroke]; 13 14 if(self.faceUp){ 15 UIImage *faceImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@", [self rankAsString], self.suit]]; 16 if (faceImage) { 17 CGRect imageRect = CGRectInset(self.bounds, 18 self.bounds.size.width * (1.0 - self.faceCardScaleFactor), 19 self.bounds.size.height * (1.0 - self.faceCardScaleFactor)); 20 [faceImage drawInRect:imageRect]; 21 } else { 22 [self drawPips]; 23 } 24 25 [self drawCorners]; 26 } else { 27 [[UIImage imageNamed:@"cardback"] drawInRect:self.bounds]; 28 } 29 30 }
1 -(void)drawCorners 2 { 3 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 4 paragraphStyle.alignment = NSTextAlignmentCenter; 5 6 UIFont *cornerFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; 7 cornerFont = [cornerFont fontWithSize:cornerFont.pointSize * [self cornerScaleFator]]; 8 9 NSAttributedString *cornerText = [[NSAttributedString alloc] 10 initWithString:[NSString stringWithFormat:@"%@ %@", [self rankAsString], self.suit] 11 attributes:@{NSFontAttributeName : cornerFont, NSParagraphStyleAttributeName :paragraphStyle}]; 12 CGRect textBounds; 13 textBounds.origin = CGPointMake([self cornerOffset], [self cornerOffset]); 14 textBounds.size = [cornerText size]; 15 [cornerText drawInRect:textBounds]; 16 17 CGContextRef context = UIGraphicsGetCurrentContext(); 18 CGContextTranslateCTM(context, self.bounds.size.width, self.bounds.size.height); 19 CGContextRotateCTM(context, M_PI); 20 [cornerText drawInRect:textBounds]; 21 }
5、添加手势有两种方法:一种是对指定view中拖入手势控件,再由控件Ctrl成相应的Action实现。另一种是先在view相关的Controller中实现公共手势方法,在主View中添加手势事件,如下:
1 //子View中实现公共手势方法(缩放手势) 2 -(void)pinch:(UIPinchGestureRecognizer *)gesture 3 { 4 if((gesture.state == UIGestureRecognizerStateChanged || 5 gesture.state == UIGestureRecognizerStateEnded)) { 6 self.faceCardScaleFactor *= gesture.scale; 7 gesture.scale = 1.0; 8 } 9 } 10 //主View中添加手势事件 11 - (void)viewDidLoad 12 { 13 [super viewDidLoad]; 14 // Do any additional setup after loading the view, typically from a nib. 15 [self.playingCardView addGestureRecognizer:[[UIPinchGestureRecognizer alloc] initWithTarget:self.playingCardView action:@selector(pinch:)]]; 16 }