01_设置颜色
- 使用 16 进制数字创建颜色,例如 0xFF0000 创建红色
- 生成随机颜色
- 使用 R / G / B 数值创建颜色
// UIColor+Addition.h
#import <UIKit/UIKit.h>
@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
02_设置文字
- label上显示的文字
- 字体大小
- 文字颜色
// UILabel+Addition.h
#import <UIKit/UIKit.h>
@interface UILabel (Addition)
/**
返回一个Label控件
@param text label上显示的文字
@param font 字体大小
@param color 文字颜色
*/
+ (UILabel *)makeLabelWithText:(NSString *)text andTextFont:(CGFloat)font andTextColor:(UIColor *)color;
@end
// UILabel+Addition.m
#import "UILabel+Addition.h"
@implementation UILabel (Addition)
+ (UILabel *)makeLabelWithText:(NSString *)text andTextFont:(CGFloat)font andTextColor:(UIColor *)color {
UILabel *label = [[UILabel alloc] init];
label.text = text;
// 设置字体大小
label.font = [UIFont systemFontOfSize:font];
// 设置文字颜色
label.textColor = color;
return label;
}
@end
待继续添加.....