博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS十六进制颜色转换成UIColor
阅读量:4086 次
发布时间:2019-05-25

本文共 1072 字,大约阅读时间需要 3 分钟。

创建UIColor的分类

//  UIColor+Addition.h#import 
@interface UIColor (Addition)/// 使用 16 进制数字创建颜色,例如 0xFF0000 创建红色////// @param hex 16 进制无符号32位整数////// @return 颜色+ (instancetype)colorWithHex:(uint32_t)hex;/// 生成随机颜色////// @return 随机颜色+ (instancetype)randomColor;/// 使用 R / G / B 数值创建颜色////// @param red red/// @param green green/// @param blue blue////// @return 颜色+ (instancetype)colorWithRed:(uint8_t)red green:(uint8_t)green blue:(uint8_t)blue;@end
//  UIColor+Addition.m#import "UIColor+Addition.h"@implementation UIColor (Addition)+ (instancetype)colorWithHex:(uint32_t)hex {    uint8_t r = (hex & 0xff0000) >> 16;    uint8_t g = (hex & 0x00ff00) >> 8;    uint8_t b = hex & 0x0000ff;    return [self colorWithRed:r green:g blue:b];}+ (instancetype)randomColor {    return [UIColor colorWithRed:arc4random_uniform(256) green:arc4random_uniform(256) blue:arc4random_uniform(256)];}+ (instancetype)colorWithRed:(uint8_t)red green:(uint8_t)green blue:(uint8_t)blue {    return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];}@end

转载地址:http://rykii.baihongyu.com/

你可能感兴趣的文章
原生JS中DOM节点相关API合集
查看>>
【TINY4412】U-BOOT移植笔记:(7)SDRAM驱动
查看>>
【TINY4412】U-BOOT移植笔记:(12)BEEP驱动
查看>>
单链表的修改和删除
查看>>
C++的三个基本特征:封装、继承、多态
查看>>
C++虚函数的总结
查看>>
什么是URL地址?
查看>>
C++多态的实现方式总结
查看>>
学习C++需要注意的问题
查看>>
C++模板
查看>>
C++双冒号(::)的用法
查看>>
【Unity】封装SQLite管理类
查看>>
【Unity】面试题整理
查看>>
【C#】如何实现一个迭代器
查看>>
【Unity】Destroy和DestroyImmediate的区别
查看>>
【Lua】Mac系统下配置SublimeText的Lua编译环境
查看>>
【C#】利用Conditional属性完成编译忽略
查看>>
【Unity】微信登录后将头像存为bytes,将bytes读取成sprite图片
查看>>
【Unity】使用GPS定位经纬度
查看>>
【UGUI/NGUI】一键换Text/Label字体
查看>>