UIColor

UIColorUIColor

大家好,欢迎来到IT知识分享网。

1、系统预设置颜色:

UIColor

代码:

新建一个类继承自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];
    }


UIColor

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];


UIColor

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/20783.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信