UIColor

1、系统预设置颜色:

代码:

新建一个类继承自UIView

//
//  MyView.h
//  07-uicolor


#import <UIKit/UIKit.h>

@interface MyView : UIView

@property(nonatomic, copy) NSString * classMethod;
@property(nonatomic, copy) NSString * colorName;
@property(nonatomic, copy) UIColor * color;
@property(nonatomic, assign) CGFloat valueR;
@property(nonatomic, assign) CGFloat valueG;
@property(nonatomic, assign) CGFloat valueB;
@property(nonatomic, assign) CGFloat valueAlpha;

@end



//
//  MyView.m
//  07-uicolor
//


#import "MyView.h"

@implementation MyView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
- (void)drawRect:(CGRect)rect {
    
    NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    
    //1
    CGRect classMethodRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width/8 * 2, rect.size.height);
    [self.classMethod drawInRect:classMethodRect withAttributes:@{NSParagraphStyleAttributeName:paragraphStyle}];
    
    //2
    CGRect colorNameRect = CGRectMake(rect.origin.x + rect.size.width/8 * 2, rect.origin.y, rect.size.width/8, rect.size.height);
    [self.colorName drawInRect:colorNameRect withAttributes:@{NSParagraphStyleAttributeName:paragraphStyle}];
    
    //3
    CGRect labelRect = CGRectMake(rect.origin.x + rect.size.width/8 * 2 + rect.size.width/8, rect.origin.y, rect.size.width/8, rect.size.height);
    UILabel * label = [[UILabel alloc] initWithFrame:labelRect];
    label.backgroundColor = self.color;
    [self addSubview:label];
    
    //4
    CGRect valueRRect = CGRectMake(rect.origin.x + rect.size.width/8 * 2 + rect.size.width/8 + rect.size.width/8, rect.origin.y, rect.size.width/8, rect.size.height);
    [[NSString stringWithFormat:@"%.2f", self.valueR] drawInRect:valueRRect withAttributes:@{NSParagraphStyleAttributeName:paragraphStyle}];
    
    //5
    CGRect valueGRect = CGRectMake(rect.origin.x + rect.size.width/8 * 2 + rect.size.width/8 + rect.size.width/8 + rect.size.width/8, rect.origin.y, rect.size.width/8, rect.size.height);
    [[NSString stringWithFormat:@"%.2f", self.valueG] drawInRect:valueGRect withAttributes:@{NSParagraphStyleAttributeName:paragraphStyle}];
    
    //6
    CGRect valueBRect = CGRectMake(rect.origin.x + rect.size.width/8 * 2 + rect.size.width/8 + rect.size.width/8 + rect.size.width/8 + rect.size.width/8, rect.origin.y, rect.size.width/8, rect.size.height);
    [[NSString stringWithFormat:@"%.2f", self.valueB] drawInRect:valueBRect withAttributes:@{NSParagraphStyleAttributeName:paragraphStyle}];
    
    //7
    CGRect valueAlphaRect = CGRectMake(rect.origin.x + rect.size.width/8 * 2 + rect.size.width/8 + rect.size.width/8 + rect.size.width/8 + rect.size.width/8 + rect.size.width/8, rect.origin.y, rect.size.width/8, rect.size.height);
    [[NSString stringWithFormat:@"%.2f", self.valueAlpha] drawInRect:valueAlphaRect withAttributes:@{NSParagraphStyleAttributeName:paragraphStyle}];
    
    
}


@end


测试代码:

    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    
    NSArray * classMethodArray = @[@"blackColor", @"darkGrayColor", @"lightGrayColor", @"whiteColor", @"grayColor", @"redColor", @"greenColor", @"blueColor", @"cyanColor", @"yellowColor", @"magentaColor", @"orangeColor", @"purpleColor", @"brownColor", @"clearColor"];
    NSArray * classNameArray = @[@"黑色", @"深灰色", @"浅灰色", @"白色", @"灰色", @"红色", @"绿色", @"蓝色", @"水色", @"黄色", @"粉红色", @"橙色", @"紫色", @"茶色", @"透明色"];
    NSArray * colorArray = @[[UIColor blackColor], [UIColor darkGrayColor], [UIColor lightGrayColor], [UIColor whiteColor], [UIColor grayColor], [UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor cyanColor], [UIColor yellowColor], [UIColor magentaColor], [UIColor orangeColor], [UIColor purpleColor], [UIColor brownColor], [UIColor clearColor]];
    CGFloat valueRArray[15] = {0.0, 0.33, 0.66, 1.0, 0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.5, 0.6, 0.0};
    CGFloat valueGArray[15] = {0.0, 0.33, 0.66, 1.0, 0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.5, 0.0, 0.4, 0.0};
    CGFloat valueBArray[15] = {0.0, 0.33, 0.66, 1.0, 0.5, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.2, 0.0};
    CGFloat valueAlphaArray[15] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0};
    
    for (int i = 0; i < 15; i++) {
        MyView * myView = [[MyView alloc] initWithFrame:CGRectMake(0, 20 + 40 * i, width, 30)];
        myView.classMethod = classMethodArray[i];
        myView.colorName = classNameArray[i];
        myView.color = colorArray[i];
        myView.valueR = valueRArray[i];
        myView.valueG = valueGArray[i];
        myView.valueB = valueBArray[i];
        myView.valueAlpha = valueAlphaArray[i];
        myView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:myView];
    }
运行后就是上面的效果。



2、其他系统颜色:

    NSArray * classMethodArray1 = @[@"lightTextColor", @"darkTextColor", @"groupTableViewBackgroundColor"];
    NSArray * classNameArray1 = @[@"灰色", @"黑色", @"灰条状背景"];
    NSArray * colorArray1 = @[[UIColor lightTextColor], [UIColor darkTextColor], [UIColor groupTableViewBackgroundColor]];
    CGFloat valueRArray1[3] = {0.6, 0.0};
    CGFloat valueGArray1[3] = {0.6, 0.0};
    CGFloat valueBArray1[3] = {0.6, 0.0};
    CGFloat valueAlphaArray1[15] = {1.0, 1.0};
    for (int i = 0; i < 3; i++) {
        MyView * myView = [[MyView alloc] initWithFrame:CGRectMake(0, 20 + 40 * i, width, 30)];
        myView.classMethod = classMethodArray1[i];
        myView.colorName = classNameArray1[i];
        myView.color = colorArray1[i];
        myView.valueR = valueRArray1[i];
        myView.valueG = valueGArray1[i];
        myView.valueB = valueBArray1[i];
        myView.valueAlpha = valueAlphaArray1[i];
        myView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:myView];
    }

3、颜色的创建:

//指定alpha值创建白色
+ (UIColor *)colorWithWhite:(CGFloat)white alpha:(CGFloat)alpha;

//指定HSB值创建UIColor,H为色相、S为彩度、B为亮度
+ (UIColor *)colorWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha;

//指定RGB值创建UIColor,RGB值的范围:0.0-1.0
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;

//指定CGColor创建UIColor
+ (UIColor *)colorWithCGColor:(CGColorRef)cgColor;

//指定背景图片创建颜色
+ (UIColor *)colorWithPatternImage:(UIImage *)image;

//下同
- (UIColor *)initWithWhite:(CGFloat)white alpha:(CGFloat)alpha;
- (UIColor *)initWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha;
- (UIColor *)initWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
- (UIColor *)initWithCGColor:(CGColorRef)cgColor;
- (UIColor *)initWithPatternImage:(UIImage*)image;

CGColor是CoreGraphics中使用的

4、使用背景图片创建颜色:

    UIImage * image = [UIImage imageNamed:@"pic4.jpg"];
    UIView * view = [[UIView alloc] initWithFrame:self.view.bounds];
    view.backgroundColor = [UIColor colorWithPatternImage:image];
    [self.view addSubview:view];




“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值