Chameleon(UIColor+Chameleon)使用

本文介绍了Chameleon颜色框架的基本用法,包括如何通过CocoaPods安装、使用系统颜色及转换为扁平化颜色、获取互补色、使用内置颜色、生成随机扁平化颜色等。同时展示了如何在iOS应用中添加不同颜色样式的视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Chameleon是一款轻量级但非常强大的颜色框架,自带24种扁平化颜色,每一种颜色均分为 flat/dark两种颜色,颜色示例如下:

下面是关于Chameleon 中的”UIColor+Chameleon”的使用:
1.使用 cocoa pods 导入”Chameleon”
2.导入头文件

#import <Chameleon.h>

3.代码部分如下:

宏定义

#define gap 15
#define lab_X 50
#define lab_H 30
#define labT_W 300
#define labC_W 100

属性

@property (nonatomic, strong) UILabel *labText1;
@property (nonatomic, strong) UILabel *labColor1;

@property (nonatomic, strong) UILabel *labText2;
@property (nonatomic, strong) UILabel *labColor2;

@property (nonatomic, strong) UILabel *labText3;
@property (nonatomic, strong) UILabel *labColor3;

@property (nonatomic, strong) UILabel *labText4;
@property (nonatomic, strong) UILabel *labColor4;

@property (nonatomic, strong) UILabel *labText5;
@property (nonatomic, strong) UILabel *labColor5;

@property (nonatomic, strong) UILabel *labText6;
@property (nonatomic, strong) UILabel *labColor6;

添加视图

//添加视图
- (void)addAllLabels
{
    //系统颜色
    [self.view addSubview:self.labText1];
    [self.view addSubview:self.labColor1];
    //系统颜色扁平化
    [self.view addSubview:self.labText2];
    [self.view addSubview:self.labColor2];
    //系统颜色取互补色
    [self.view addSubview:self.labText3];
    [self.view addSubview:self.labColor3];
    //Chameleon 自带颜色
    [self.view addSubview:self.labText4];
    [self.view addSubview:self.labColor4];
    //渐变色使用
    [self.view addSubview:self.labText5];
    [self.view addSubview:self.labColor5];
    //随机扁平化颜色
    [self.view addSubview:self.labText6];
    [self.view addSubview:self.labColor6];
}

定义一个系统色

UIColor *colorSystem = [UIColor greenColor];
self.labColor1.backgroundColor = colorSystem;

将颜色转换为扁平化颜色

//扁平化颜色
self.labColor2.backgroundColor = [colorSystem flatten];

取互补色

//取互补色
self.labColor3.backgroundColor = [UIColor colorWithComplementaryFlatColorOf:[colorSystem flatten] withAlpha:1.0];

Chameleon自带扁平化颜色使用

//使用自带的颜色
self.labColor4. backgroundColor = FlatCoffee;

使用随机扁平化颜色

//随机扁平化颜色
self.labColor6.backgroundColor = [UIColor randomFlatColor];

getter

- (UILabel *)labText1
{
    if (!_labText1)
    {
        _labText1 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, 80, labT_W, lab_H)];
        _labText1.text = @"系统颜色";
    }
    return _labText1;
}

- (UILabel *)labColor1
{
    if (!_labColor1)
    {
        _labColor1 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, CGRectGetMaxY(_labText1.frame) + gap, labC_W, lab_H)];
    }
    return _labColor1;
}

- (UILabel *)labText2
{
    if (!_labText2)
    {
        _labText2 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, CGRectGetMaxY(_labColor1.frame) + gap, labT_W, lab_H)];
        _labText2.text = @"系统颜色扁平化后";
    }
    return _labText2;
}

- (UILabel *)labColor2
{
    if (!_labColor2)
    {
        _labColor2 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, CGRectGetMaxY(_labText2.frame) + gap, labC_W, lab_H)];
    }
    return _labColor2;
}

- (UILabel *)labText3
{
    if (!_labText3)
    {
        _labText3 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, CGRectGetMaxY(_labColor2.frame) + gap, labT_W, lab_H)];
        _labText3.text = @"取互补色";
    }
    return _labText3;
}

- (UILabel *)labColor3
{
    if (!_labColor3)
    {
        _labColor3 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, CGRectGetMaxY(_labText3.frame) + gap, labC_W, lab_H)];
    }
    return _labColor3;
}

- (UILabel *)labText4
{
    if (!_labText4)
    {
        _labText4 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, CGRectGetMaxY(_labColor3.frame) + gap, labT_W, lab_H)];
        _labText4.text = @"使用框架自带的FlatColor--FlatCoffee";
    }
    return _labText4;
}

- (UILabel *)labColor4
{
    if (!_labColor4)
    {
        _labColor4 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, CGRectGetMaxY(_labText4.frame) + gap, labC_W, lab_H)];
    }
    return _labColor4;
}

- (UILabel *)labText5
{
    if (!_labText5)
    {
        _labText5 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, CGRectGetMaxY(_labColor4.frame) + gap, labT_W, lab_H)];
        _labText5.text = @"渐变色的使用";
    }
    return _labText5;
}

- (UILabel *)labColor5
{
    if (!_labColor5)
    {
        _labColor5 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, CGRectGetMaxY(_labText5.frame) + gap, labC_W, lab_H)];
    }
    return _labColor5;
}

- (UILabel *)labText6
{
    if (!_labText6)
    {
        _labText6 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, CGRectGetMaxY(_labColor5.frame) + gap, labT_W, lab_H)];
        _labText6.text = @"随机扁平化颜色";
    }
    return _labText6;
}

- (UILabel *)labColor6
{
    if (!_labColor6)
    {
        _labColor6 = [[UILabel alloc] initWithFrame:CGRectMake(lab_X, CGRectGetMaxY(_labText6.frame) + gap, labC_W, lab_H)];
    }
    return _labColor6;
}

运行效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值