
效果如下:

ViewController.h
1 #import <UIKit/UIKit.h>
2
3 @interface ViewController : UIViewController {
4 @PRivate
5 UILabel *lblMessage;
6 CGFloat colorRed;
7 CGFloat colorGreen;
8 CGFloat colorBlue;
9 }
10
11 @end
ViewController.m
1 #import "ViewController.h"
2
3 @interface ViewController ()
4 - (void)redDidPush;
5 - (void)greenDidPush;
6 - (void)blueDidPush;
7 - (void)changeLabelColor:(CGFloat*)pColor;
8 @end
9
10 @implementation ViewController
11
12 #pragma mark - Start Implementation For Methods
13 - (void)viewDidLoad {
14 [super viewDidLoad];
15 colorRed = 0.0;
16 colorGreen = 0.0;
17 colorBlue = 0.0;
18 CGPoint newPoint = self.view.center;
19
20 //追加Label标签
21 lblMessage = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
22 lblMessage.textAlignment = NSTextAlignmentCenter;
23 lblMessage.center = newPoint;
24 lblMessage.textColor = [UIColor whiteColor];
25 lblMessage.text = @"染上新的颜色吧...";
26 lblMessage.backgroundColor = [[UIColor alloc] initWithRed:colorRed green:colorGreen blue:colorBlue alpha:1.0];
27 //设置Label标签的圆角
28 //默认是四个圆角
29 // lblMessage.layer.cornerRadius = 20.0;
30 // lblMessage.layer.masksToBounds = YES;
31
32 /*
33 typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
34 UIRectCornerTopLeft = 1 << 0,
35 UIRectCornerTopRight = 1 << 1,
36 UIRectCornerBottomLeft = 1 << 2,
37 UIRectCornerBottomRight = 1 << 3,
38 UIRectCornerAllCorners = ~0UL
39 };
40 */
41 //这里使用Layer实现两个圆角;左上角和右上角
42 UIRectCorner rectCorner = UIRectCornerTopLeft | UIRectCornerTopRight;
43 UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:lblMessage.bounds
44 byRoundingCorners:rectCorner
45 cornerRadii:CGSizeMake(20.0, 20.0)];
46 CAShapeLayer *shapeLayer = [CAShapeLayer layer];
47 shapeLayer.path = path.CGPath;
48 lblMessage.layer.mask = shapeLayer;
49 [self.view addSubview:lblMessage];
50
51 //追加红色按钮
52 UIButton *btnRed = [UIButton buttonWithType:UIButtonTypeRoundedRect];
53 btnRed.frame = CGRectMake(0, 0, 50, 40);
54 newPoint.x -= (btnRed.frame.size.width + 10);
55 newPoint.y = self.view.frame.size.height - 70;
56 btnRed.center = newPoint;
57 btnRed.backgroundColor = [UIColor grayColor];
58 [btnRed setTitle:@"红" forState:UIControlStateNormal];
59 [btnRed setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
60 [btnRed addTarget:self action:@selector(redDidPush) forControlEvents:UIControlEventTouchUpInside];
61 [self.view addSubview:btnRed];
62
63 //追加绿色按钮
64 UIButton *btnGreen = [UIButton buttonWithType:UIButtonTypeRoundedRect];
65 btnGreen.frame = btnRed.frame;
66 newPoint.x += (btnRed.frame.size.width + 5);
67 btnGreen.center = newPoint;
68 btnGreen.backgroundColor = [UIColor grayColor];
69 [btnGreen setTitle:@"绿" forState:UIControlStateNormal];
70 [btnGreen setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
71 [btnGreen addTarget:self action:@selector(greenDidPush) forControlEvents:UIControlEventTouchUpInside];
72 [self.view addSubview:btnGreen];
73
74 //追加蓝色按钮
75 UIButton *btnBlue = [UIButton buttonWithType:UIButtonTypeRoundedRect];
76 btnBlue.frame = btnRed.frame;
77 newPoint.x += (btnRed.frame.size.width + 5);
78 btnBlue.center = newPoint;
79 btnBlue.backgroundColor = [UIColor grayColor];
80 [btnBlue setTitle:@"蓝" forState:UIControlStateNormal];
81 [btnBlue setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
82 [btnBlue addTarget:self action:@selector(blueDidPush) forControlEvents:UIControlEventTouchUpInside];
83 [self.view addSubview:btnBlue];
84 }
85
86 - (void)didReceiveMemoryWarning {
87 [super didReceiveMemoryWarning];
88 // Dispose of any resources that can be recreated.
89 }
90
91 #pragma mark - Private Methods
92 - (void)redDidPush {
93 [self changeLabelColor:&colorRed];
94 }
95
96 - (void)greenDidPush {
97 [self changeLabelColor:&colorGreen];
98 }
99
100 - (void)blueDidPush {
101 [self changeLabelColor:&colorBlue];
102 }
103
104 - (void)changeLabelColor:(CGFloat*)pColor {
105 if (pColor) {
106 if (*pColor > 0.99) {
107 *pColor = 0.0;
108 } else {
109 *pColor += 0.1;
110 }
111 lblMessage.backgroundColor = [[UIColor alloc] initWithRed:colorRed green:colorGreen blue:colorBlue alpha:1.0];
112 }
113 }
114
115 @end