iOS常用的基础分类(Category)-让开发更简单

在工作中总结了一些开发中常用到的分类,使用这些分类能使开发效率更高,让代码变得更简洁,减少了很多不必要的代码量,直接拖到项目中即可. 下载地址:https://github.com/LDisguiser/Category

注意 : 在项目的PCH文件中导入LHAdditions.h即可

UILabel+LHAddition

/**
 快速初始化一个Label

 @param text 文本内容
 @param fontName 字体样式 nil就是默认系统样式
 @param fontSize 字体大小
 @param colorString 字体颜色
 @return Label
 */
+ (instancetype)labelWithText:(NSString *)text fontName:(nullable NSString *)fontName fontSize:(CGFloat)fontSize colorString:(NSString *)colorString;

UIButton+LHAddition

/**
 快速创建按钮

 @param title 标题
 @param fontName 标题样式
 @param fontSize 标题大小
 @param normalColor 默认颜色
 @param selectedColor 选中颜色
 @return 按钮
 */
+ (instancetype)buttonWithTitleString:(NSString *)title fontName:(nullable NSString *)fontName fontSize:(CGFloat)fontSize normalColor:(NSString *)normalColor selectedColor:(NSString *)selectedColor;

UIColor+LHAddition

/**
 使用 16 进制数字创建颜色,例如 0xFF0000 创建红色

 @param hex 16 进制无符号32位整数
 @return 颜色
 */
+ (instancetype)colorWithHex:(uint32_t)hex;

/**
 产生随机色

 @return 随机色
 */
+ (instancetype)aarandomColor;

/**
 支持三种格式颜色字符串@"123456"  @"0x123456"  @"#123456"输入

 @param color 十六进制颜色字符串
 @param alpha 透明度
 @return Color
 */
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;

/**
 支持三种格式颜色字符串@"123456"  @"0x123456"  @"#123456"输入

 @param color 十六进制颜色字符串
 @return Color
 */
+ (UIColor *)colorWithHexString:(NSString *)color;

NSString+path

/// 获取Documents目录
+ (NSString *)getDocumentsPath;
/// 获取Cache目录
+ (NSString *)getCachePath;
/// 获取tmp目录
+ (NSString *)getTempPath;

#pragma mark -- 根据url的最后一部分,来命名保存该url内容的路径
/// 保存在Documents目录下
- (NSString *)appendDocumentsPath;
/// 保存在Cache目录下
- (NSString *)appendCachePath;
/// 保存在Tmp目录下
- (NSString *)appendTempPath;

UIView+LHAddition

/// 返回屏幕截图
- (UIImage *)snapshotImage;

/// 找到自己所在的控制器,判断是否存在之后,使用
- (UIViewController *)viewController;

#pragma mark -- 基础属性
@property (nonatomic) CGFloat left;

/**
 * Shortcut for frame.origin.y
 *
 * Sets frame.origin.y = top
 */
@property (nonatomic) CGFloat top;

/**
 * Shortcut for frame.origin.x + frame.size.width
 *
 * Sets frame.origin.x = right - frame.size.width
 */
@property (nonatomic) CGFloat right;

/**
 * Shortcut for frame.origin.y + frame.size.height
 *
 * Sets frame.origin.y = bottom - frame.size.height
 */
@property (nonatomic) CGFloat bottom;

/**
 * Shortcut for frame.size.width
 *
 * Sets frame.size.width = width
 */
@property (nonatomic) CGFloat width;

/**
 * Shortcut for frame.size.height
 *
 * Sets frame.size.height = height
 */
@property (nonatomic) CGFloat height;

/**
 * Shortcut for center.x
 *
 * Sets center.x = centerX
 */
@property (nonatomic) CGFloat centerX;

/**
 * Shortcut for center.y
 *
 * Sets center.y = centerY
 */
@property (nonatomic) CGFloat centerY;
/**
 * Shortcut for frame.origin
 */
@property (nonatomic) CGPoint origin;

/**
 * Shortcut for frame.size
 */
@property (nonatomic) CGSize size;

#pragma mark -- 渐变的颜色设置
/* The array of CGColorRef objects defining the color of each gradient
 * stop. Defaults to nil. Animatable. */

@property(nullable, copy) NSArray *colors;

/* An optional array of NSNumber objects defining the location of each
 * gradient stop as a value in the range [0,1]. The values must be
 * monotonically increasing. If a nil array is given, the stops are
 * assumed to spread uniformly across the [0,1] range. When rendered,
 * the colors are mapped to the output colorspace before being
 * interpolated. Defaults to nil. Animatable. */

@property(nullable, copy) NSArray<NSNumber *> *locations;

/* The start and end points of the gradient when drawn into the layer's
 * coordinate space. The start point corresponds to the first gradient
 * stop, the end point to the last gradient stop. Both points are
 * defined in a unit coordinate space that is then mapped to the
 * layer's bounds rectangle when drawn. (I.e. [0,0] is the bottom-left
 * corner of the layer, [1,1] is the top-right corner.) The default values
 * are [.5,0] and [.5,1] respectively. Both are animatable. */

@property CGPoint startPoint;
@property CGPoint endPoint;

+ (UIView *_Nullable)gradientViewWithColors:(NSArray<UIColor *> *_Nullable)colors locations:(NSArray<NSNumber *> *_Nullable)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint;

- (void)setGradientBackgroundWithColors:(NSArray<UIColor *> *_Nullable)colors locations:(NSArray<NSNumber *> *_Nullable)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint;

NSObject+EmptyJudgemen

/// 校验一个字符串(附带去掉首尾空格),字典,数组是否为空
- (BOOL)EmptyJudgemen;

NSObject+LH_Addition

/**
 字典转模型  -- 模型对象中只需要写好对应属性即可
 
 @param dict 需要转换的字典
 @return 返回模型对象
 */
+ (instancetype)model_ObjectWithDict:(NSDictionary *)dict;

NSArray+Log, Xcode中文打印字典,数组等信息.

NSString+Hash

#pragma mark - 散列函数
/**
 *  计算MD5散列结果
 *
 *  终端测试命令:
 *  @code
 *  md5 -s "string"
 *  @endcode
 *
 *  <p>提示:随着 MD5 碰撞生成器的出现,MD5 算法不应被用于任何软件完整性检查或代码签名的用途。<p>
 *
 *  @return 32个字符的MD5散列字符串
 */
- (NSString *)md5String;

/**
 *  计算SHA1散列结果
 *
 *  终端测试命令:
 *  @code
 *  echo -n "string" | openssl sha -sha1
 *  @endcode
 *
 *  @return 40个字符的SHA1散列字符串
 */
- (NSString *)sha1String;

/**
 *  计算SHA256散列结果
 *
 *  终端测试命令:
 *  @code
 *  echo -n "string" | openssl sha -sha256
 *  @endcode
 *
 *  @return 64个字符的SHA256散列字符串
 */
- (NSString *)sha256String;

/**
 *  计算SHA 512散列结果
 *
 *  终端测试命令:
 *  @code
 *  echo -n "string" | openssl sha -sha512
 *  @endcode
 *
 *  @return 128个字符的SHA 512散列字符串
 */
- (NSString *)sha512String;

#pragma mark - HMAC 散列函数
/**
 *  计算HMAC MD5散列结果
 *
 *  终端测试命令:
 *  @code
 *  echo -n "string" | openssl dgst -md5 -hmac "key"
 *  @endcode
 *
 *  @return 32个字符的HMAC MD5散列字符串
 */
- (NSString *)hmacMD5StringWithKey:(NSString *)key;

/**
 *  计算HMAC SHA1散列结果
 *
 *  终端测试命令:
 *  @code
 *  echo -n "string" | openssl sha -sha1 -hmac "key"
 *  @endcode
 *
 *  @return 40个字符的HMAC SHA1散列字符串
 */
- (NSString *)hmacSHA1StringWithKey:(NSString *)key;

/**
 *  计算HMAC SHA256散列结果
 *
 *  终端测试命令:
 *  @code
 *  echo -n "string" | openssl sha -sha256 -hmac "key"
 *  @endcode
 *
 *  @return 64个字符的HMAC SHA256散列字符串
 */
- (NSString *)hmacSHA256StringWithKey:(NSString *)key;

/**
 *  计算HMAC SHA512散列结果
 *
 *  终端测试命令:
 *  @code
 *  echo -n "string" | openssl sha -sha512 -hmac "key"
 *  @endcode
 *
 *  @return 128个字符的HMAC SHA512散列字符串
 */
- (NSString *)hmacSHA512StringWithKey:(NSString *)key;

#pragma mark - 文件散列函数

/**
 *  计算文件的MD5散列结果
 *
 *  终端测试命令:
 *  @code
 *  md5 file.dat
 *  @endcode
 *
 *  @return 32个字符的MD5散列字符串
 */
- (NSString *)fileMD5Hash;

/**
 *  计算文件的SHA1散列结果
 *
 *  终端测试命令:
 *  @code
 *  openssl sha -sha1 file.dat
 *  @endcode
 *
 *  @return 40个字符的SHA1散列字符串
 */
- (NSString *)fileSHA1Hash;

/**
 *  计算文件的SHA256散列结果
 *
 *  终端测试命令:
 *  @code
 *  openssl sha -sha256 file.dat
 *  @endcode
 *
 *  @return 64个字符的SHA256散列字符串
 */
- (NSString *)fileSHA256Hash;

/**
 *  计算文件的SHA512散列结果
 *
 *  终端测试命令:
 *  @code
 *  openssl sha -sha512 file.dat
 *  @endcode
 *
 *  @return 128个字符的SHA512散列字符串
 */
- (NSString *)fileSHA512Hash;

.End

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容