有木有觉得UIColor自带的那几个用名称直接调用的颜色很少很难看呐?
先前用循环的方法生成了渐变的彩虹色,但这些颜色不是色彩的全部,也不是很符合设计的美学选择。
网上Web Color表有很多也非常好找,但是对它进行OC实现的例子却很少见。
还是那句老话,不好找就自己动手啦。
今天为大家献上的这段代码是对Web Color的OC实现,下面是程序模拟器执行效果图和源代码。
核心自定义类是WebColor和ColorModel,首先我们来看一看这两个类是怎么使用的。
//
// RootViewController.m
// MyColors
//
// Created by 杜子兮 on 14-1-7.
// Copyright (c) 2014年 莲兮奈若何. All rights reserved.
//
#import "RootViewController.h"
#import "WebColor.h"
#import "ColorModel.h"
#define OY 60
@implementation RootViewController
- (void)viewDidLoad{
[self.view setBackgroundColor:[WebColor webColorWithName:@"hotPink"]]; //<----可以用英文名调用
UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 320, 34)];
titleLabel.text = @"Web Color 一览";
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [WebColor crimson]; //<-----还可以像调用系统颜色一样使用!!
titleLabel.backgroundColor = [WebColor webColorWithName:@"粉红"]; //<----用中文名调用也可以的哦!
[self.view addSubview:titleLabel];
//下面展示所有的颜色列表
WebColor * webColor = [[WebColor alloc]init];
CGRect rect = self.view.frame;
rect.origin.y += OY;
rect.origin.x += 5;
rect.size.width -= 10;
rect.size.height -= OY + 5;
UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:rect];
scroll.contentSize = CGSizeMake(rect.size.width, 20 * webColor.colors.count);
[scroll setBackgroundColor:[UIColor whiteColor]]; // <------这个是系统提供的调用方法
for (int i = 0; i < webColor.colors.count; i++) {
ColorModel * colorModel = webColor.colors[i];
//英语名
UILabel * labelEn = [[UILabel alloc]initWithFrame:CGRectMake(10, 20 * i + 5, 130, 18)];
labelEn.text = colorModel.name;
labelEn.textAlignment = NSTextAlignmentRight;
labelEn.font = [UIFont fontWithName:@"Arial" size:13];
[scroll addSubview:labelEn];
//颜色块
UIButton * bt = [[UIButton alloc]initWithFrame:CGRectMake(145, 20 * i + 5, 20, 18)];
[bt setBackgroundColor:[colorModel getColor]];
[bt addTarget:self action:@selector(setBackColor:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:bt];
//中文名
UILabel * labelCn = [[UILabel alloc]initWithFrame:CGRectMake(170, 20 * i + 5, 200, 18)];
labelCn.font = [UIFont fontWithName:@"Arial" size:13];
labelCn.text = colorModel.nameCN;
[scroll addSubview:labelCn];
}
[self.view addSubview:scroll];
}
- (void) setBackColor:(id)sender{
UIButton * bt = (UIButton *) sender;
[self.view setBackgroundColor:bt.backgroundColor];
}
@end
//
// WebColor.h
// MyColors
//
// Created by 杜子兮 on 14-1-7.
// Copyright (c) 2014年 莲兮奈若何. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface WebColor : NSObject
@property(nonatomic,retain) NSMutableArray * colors;
- (UIColor *) getColorWithName:(NSString *) name;
+ (UIColor *) webColorWithName:(NSString *) name;
+ (UIColor *) lightPink;
+ (UIColor *) pink;
+ (UIColor *) crimson;
+ (UIColor *) lavenderBlush;
+ (UIColor *) paleVoiletRed;
+ (UIColor *) hotPink;
+ (UIColor *) deepPink;
+ (UIColor *) mediumVoiletRed;
+ (UIColor *) orchid;
+ (UIColor *) thistle;
+ (UIColor *) plum;
+ (UIColor *) violet;
+ (UIColor *) magenta;
+ (UIColor *) fuchsia;
+ (UIColor *) darkMagenta;
+ (UIColor *) purple;
+ (UIColor *) mediumOrchid;
+ (UIColor *) darkViolet;
+ (UIColor *) indigo;
+ (UIColor *) blueViolet;
+ (UIColor *) mediumPurple;
+ (UIColor *) mediumSlateBlue;
+ (UIColor *) slateBlue;
+ (UIColor *) darkSlateBlue;
+ (UIColor *) lavender;
+ (UIColor *) ghostWhite;
+ (UIColor *) blue;
+ (UIColor *) mediumBlue;
+ (UIColor *) midnightBlue;
+ (UIColor *) darkBlue;
+ (UIColor *) navy;
+ (UIColor *) royalBlue;
+ (UIColor *) cornflowerBlue;
+ (UIColor *) lightSteelBlue;
+ (UIColor *) lightSlateGray;
+ (UIColor *) slateGray;
+ (UIColor *) dodgerBlue;
+ (UIColor *) aliceBlue;
+ (UIColor *) steelBlue;
+ (UIColor *) lightSkyBlue;
+ (UIColor *) skyBlue;
+ (UIColor *) deepSkyBlue;
+ (UIColor *) lightBlue;
+ (UIColor *) powderBlue;
+ (UIColor *) cadetBlue;
+ (UIColor *) azure;
+ (UIColor *) lightCyan;
+ (UIColor *) paleTurquoise;
+ (UIColor *) cyan;
+ (UIColor *) aqua;
+ (UIColor *) darkTurquoise;
+ (UIColor *) darkSlateGray;
+ (UIColor *) darkCyan;
+ (UIColor *) teal;
+ (UIColor *) mediumTurquoise;
+ (UIColor *) lightSeaGreen;
+ (UIColor *) turquoise;
+ (UIColor *) aquamarine;
+ (UIColor *) mediumAquamarine;
+ (UIColor *) mediumSpringGreen;
+ (UIColor *) mintCream;
+ (UIColor *) springGreen;
+ (UIColor *) mediumSeaGreen;
+ (UIColor *) seaGreen;
+ (UIColor *) honeydew;
+ (UIColor *) lightGreen;
+ (UIColor *) paleGreen;
+ (UIColor *) darkSeaGreen;
+ (UIColor *) limeGreen;
+ (UIColor *) lime;
+ (UIColor *) forestGreen;
+ (UIColor *) green;
+ (UIColor *) darkGreen;
+ (UIColor *) chartreuse;
+ (UIColor *) lawnGreen;
+ (UIColor *) greenYellow;
+ (UIColor *) darkOliveGreen;
+ (UIColor *) yellowGreen;
+ (UIColor *) oliveDrab;
+ (UIColor *) beige;
+ (UIColor *) lightGoldenrodYellow;
+ (UIColor *) ivory;
+ (UIColor *) lightYellow;
+ (UIColor *) yellow;
+ (UIColor *) olive;
+ (UIColor *) darkKhaki;
+ (UIColor *) lemonChiffon;
+ (UIColor *) paleGoldenrod;
+ (UIColor *) khaki;
+ (UIColor *) gold;
+ (UIColor *) cornsilk;
+ (UIColor *) goldenrod;
+ (UIColor *) darkGoldenrod;
+ (UIColor *) floralWhite;
+ (UIColor *) oldLace;
+ (UIColor *) wheat;
+ (UIColor *) mocassin;
+ (UIColor *) orange;
+ (UIColor *) papayaWhip;
+ (UIColor *) blanchedAlmond;
+ (UIColor *) navajoWhite;
+ (UIColor *) antiqueWhite;
+ (UIColor *) tan;
+ (UIColor *) burlyWood;
+ (UIColor *) bisque;
+ (UIColor *) darkOrange;
+ (UIColor *) linen;
+ (UIColor *) peru;
+ (UIColor *) peachBuff;
+ (UIColor *) sandyBrown;
+ (UIColor *) chocolate;
+ (UIColor *) saddleBrown;
+ (UIColor *) seaShell;
+ (UIColor *) sienna;
+ (UIColor *) lightSalmon;
+ (UIColor *) coral;
+ (UIColor *) orangeRed;
+ (UIColor *) darkSalmon;
+ (UIColor *) tomtato;
+ (UIColor *) mistyRose;
+ (UIColor *) salmon;
+ (UIColor *) snow;
+ (UIColor *) lightCoral;
+ (UIColor *) rosyBrown;
+ (UIColor *) indianRed;
+ (UIColor *) red;
+ (UIColor *) brown;
+ (UIColor *) fireBrick;
+ (UIColor *) darkRed;
+ (UIColor *) maroon;
+ (UIColor *) white;
+ (UIColor *) whiteSmoke;
+ (UIColor *) gainsboro;
+ (UIColor *) lightGray;
+ (UIColor *) silver;
+ (UIColor *) gray;
+ (UIColor *) darkGray;
+ (UIColor *) dimGray;
+ (UIColor *) black;
@end
//
// WebColor.m
// MyColors
//
// Created by 杜子兮 on 14-1-7.
// Copyright (c) 2014年 莲兮奈若何. All rights reserved.
//
#import "WebColor.h"
#import "ColorModel.h"
@implementation WebColor
- (id) init{
self = [super init];
if (self) {
//初始化颜色数组
_colors = [[NSMutableArray alloc]init];
[_colors addObject:[ColorModel colorModelWithName:@"lightPink" nameCN:@"浅粉红" r:255 g:182 b:193]];
[_colors addObject:[ColorModel colorModelWithName:@"pink" nameCN:@"粉红" r:255 g:192 b:203]];
[_colors addObject:[ColorModel colorModelWithName:@"crimson" nameCN:@"杏红" r:220 g:20 b:60]];
[_colors addObject:[ColorModel colorModelWithName:@"lavenderBlush" nameCN:@"脸红的淡紫色" r:255 g:240 b:245]];
[_colors addObject:[ColorModel colorModelWithName:@"paleVoiletRed" nameCN:@"苍白的紫罗兰红色" r:219 g:112 b:147]];
[_colors addObject:[ColorModel colorModelWithName:@"hotPink" nameCN:@"热情的分红" r:255 g:105 b:180]];
[_colors addObject:[ColorModel colorModelWithName:@"deepPink" nameCN:@"深粉红" r:255 g:20 b:147]];
[_colors addObject:[ColorModel colorModelWithName:@"mediumVoiletRed" nameCN:@"适中的紫罗兰红色" r:199 g:21 b:133]];
[_colors addObject:[ColorModel colorModelWithName:@"orchid" nameCN:@"兰花的紫色" r:218 g:112 b:214]];
[_colors addObject:[ColorModel colorModelWithName:@"thistle" nameCN:@"蓟" r:216 g:191 b:216]];
[_colors addObject:[ColorModel colorModelWithName:@"plum" nameCN:@"李子" r:221 g:160 b:221]];
[_colors addObject:[ColorModel colorModelWithName:@"violet" nameCN:@"紫罗兰" r:238 g:130 b:238]];
[_colors addObject:[ColorModel colorModelWithName:@"magenta" nameCN:@"洋红" r:255 g:0 b:255]];
[_colors addObject:[ColorModel colorModelWithName:@"fuchsia" nameCN:@"紫红色" r:255 g:0 b:225]];
[_colors addObject:[ColorModel colorModelWithName:@"darkMagenta" nameCN:@"紫洋红色" r:139 g:0 b:139]];
[_colors addObject:[ColorModel colorModelWithName:@"purple" nameCN:@"紫色" r:128 g:0 b:128]];
[_colors addObject:[ColorModel colorModelWithName:@"mediumOrchid" nameCN:@"适中的兰花紫" r:186 g:85 b:211]];
[_colors addObject:[ColorModel colorModelWithName:@"darkViolet" nameCN:@"深紫罗兰色" r:148 g:0 b:211]];
[_colors addObject:[ColorModel colorModelWithName:@"indigo" nameCN:@"靛青" r:75 g:0 b:130]];
[_colors addObject:[ColorModel colorModelWithName:@"blueViolet" nameCN:@"紫罗兰的蓝色