YYKit三方库常用方法

【YYKit三方库常用方法】以下是本人在浏览YYKit框架时感觉可以用到的方法:
NSArray

// 随机在数组里去一个元素,传空数组返回nil - (nullable id)randomObject; // 通过下标选取元素,下标越界也不会崩溃,下标越界返回nil - (nullable id)objectOrNilAtIndex:(NSUInteger)index; // 将 NSString/NSNumber/NSDictionary/NSArray 转为 json字符串,不进行系列化(不删除空格和回车) - (nullable NSString *)jsonStringEncoded; // 将数据进行序列化json格式转换,删除空格回车等 - (nullable NSString *)jsonPrettyStringEncoded;

NSMutableArray
// 删除数组第一个元素,并把被删除的元素返回 - (nullable id)popFirstObject; // 删除数组最后一个元素,并把被删除的元素返回 - (nullable id)popLastObject; // 增加一个非空类型,如果为空会报错 - (void)appendObject:(id)anObject; // 插入一个非空类型 到数组的首位 - (void)prependObject:(id)anObject; // 增加元素为另一个数组的全部元素,数组为空,方法不执行 - (void)appendObjects:(NSArray *)objects; // 将要增加的数组元素放到首位,数组为空,方法不执行 - (void)prependObjects:(NSArray *)objects; // 将数组元素添加到指定位置 - (void)insertObjects:(NSArray *)objects atIndex:(NSUInteger)index; // 将数组元素倒序输出 - (void)reverse; // 将数组元素随机排列 - (void)shuffle;

NSDictionary
// 将字典键排序,升序排序 - (NSArray *)allKeysSorted; // values根据键升序排序 - (NSArray *)allValuesSortedByKeys; // 判断键在字典中是否有值,键为空返回NO,有键对应的值返回YES - (BOOL)containsObjectForKey:(id)key; // 根据数组元素返回相应的键值新字典,如果键对应值为空则为nil - (NSDictionary *)entriesForKeys:(NSArray *)keys; // 将xml语句返回为字典,键值对是怎么弄得,没看懂 + (nullable NSDictionary *)dictionaryWithXML:(id)xmlDataOrString; // 根据键删除值,并把值返回 - (nullable id)popObjectForKey:(id)aKey; // 根据字典元素删除字典元素,并把被删除的值以字典返回 - (NSDictionary *)popEntriesForKeys:(NSArray *)keys;

NSString
// 将字符串以base64编码 - (nullable NSString *)base64EncodedString; // 将base64编码字符串转为utf8编码字符串 + (nullable NSString *)stringWithBase64EncodedString:(NSString *)base64EncodedString; // 根据字体字号大小,设置的宽高,换行模式,返回重写的宽高 - (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode; // 传入字号大小,默认宽高字体最大,返回字体的宽 - (CGFloat)widthForFont:(UIFont *)font; // 传入字号大小,给定宽度,不换行,返回字体高度 - (CGFloat)heightForFont:(UIFont *)font width:(CGFloat)width; // 删除字符串中的空格和换行 - (NSString *)stringByTrim; // 将字符串转化成xxx@(scale)x; 例:@"name" to @"name@2x" 在末尾追加 - (NSString *)stringByAppendingNameScale:(CGFloat)scale; // 判断字符串中是否有空格 - (BOOL)isNotBlank; // 将字符串转化为number类型 - (nullable NSNumber *)numberValue; // 将字符串转化为data类型 - (nullable NSData *)dataValue; // 返回一个range 0到字符串长度 - (NSRange)rangeOfAll; // 将json字符串,转成字典@"{"name":"a","count":2}"=> NSDictionary: @[@"name":@"a",@"count":@2] - (nullable id)jsonValueDecoded;

NSAttributedString
// 将数据归档成data数据 - (nullable NSData *)archiveToData; // 将data数据解档为字符串 + (nullable instancetype)unarchiveFromData:(NSData *)data; // 给定范围的字体,设置字号大小 - (void)setFont:(nullable UIFont *)font range:(NSRange)range; // 设置字距 @property (nullable, nonatomic, strong, readwrite) NSNumber *kern; // 给指定范围的字体设置字距 - (void)setKern:(nullable NSNumber *)kern range:(NSRange)range; // 整个字体的颜色 @property (nullable, nonatomic, strong, readwrite) UIColor *color; // 给定范围设置字体颜色 - (void)setColor:(nullable UIColor *)color range:(NSRange)range; // 设置整个字体的背景颜色 @property (nullable, nonatomic, strong, readwrite) UIColor *backgroundColor; // 给定范围设置字体的背景 - (void)setBackgroundColor:(nullable UIColor *)backgroundColor range:(NSRange)range; // 描边 @property (nullable, nonatomic, strong, readwrite) NSNumber *strokeWidth; // 给定范围,增加描边 - (void)setStrokeWidth:(nullable NSNumber *)strokeWidth range:(NSRange)range; // 描边的颜色 @property (nullable, nonatomic, strong, readwrite) UIColor *strokeColor; // 给定范围,描边的颜色 - (void)setStrokeColor:(nullable UIColor *)strokeColor range:(NSRange)range; // 文字阴影 @property (nullable, nonatomic, strong, readwrite) NSShadow *shadow; // 给定范围文字的颜色 - (void)setShadow:(nullable NSShadow *)shadow range:(NSRange)range; // 删除线的样式 @property (nonatomic, readwrite) NSUnderlineStyle strikethroughStyle; // 给定范围,删除线的颜色 - (void)setStrikethroughStyle:(NSUnderlineStyle)strikethroughStyle range:(NSRange)range; // 删除线的颜色 @property (nullable, nonatomic, strong, readwrite) UIColor *strikethroughColor; // 给定范围删除线的颜色 - (void)setStrikethroughColor:(nullable UIColor *)strikethroughColor range:(NSRange)range NS_AVAILABLE_IOS(7_0); // 下划线样式 @property (nonatomic, readwrite) NSUnderlineStyle underlineStyle; // 给定范围,下划线的样式 - (void)setUnderlineStyle:(NSUnderlineStyle)underlineStyle range:(NSRange)range; // 下划线样式 @property (nullable, nonatomic, strong, readwrite) UIColor *underlineColor; // 给定范围,下划线的颜色 - (void)setUnderlineColor:(nullable UIColor *)underlineColor range:(NSRange)range; - ....省略一些,不是太用// 文字对齐方式 @property (nonatomic, readwrite) NSTextAlignment alignment; // 给定范围的文字对齐方式 - (void)setAlignment:(NSTextAlignment)alignment range:(NSRange)range; ---Convenience methods for text highlight---// 范围,颜色,背景色,文字个性化设置 比如:`NSTextEffectAttributeName`等属性,自己百度查; 点击事件,长按事件,没有默认传nil - (void)setTextHighlightRange:(NSRange)range color:(nullable UIColor *)color backgroundColor:(nullable UIColor *)backgroundColor userInfo:(nullable NSDictionary *)userInfo tapAction:(nullable YYTextAction)tapAction longPressAction:(nullable YYTextAction)longPressAction; // 范围,颜色,背景色,点击事件 - (void)setTextHighlightRange:(NSRange)range color:(nullable UIColor *)color backgroundColor:(nullable UIColor *)backgroundColor tapAction:(nullable YYTextAction)tapAction; // 范围,颜色,背景色,字体个性设置 - (void)setTextHighlightRange:(NSRange)range color:(nullable UIColor *)color backgroundColor:(nullable UIColor *)backgroundColor userInfo:(nullable NSDictionary *)userInfo; // 插入字符串到指定位置 - (void)insertString:(NSString *)string atIndex:(NSUInteger)location; // 增加字符串到末尾 - (void)appendString:(NSString *)string;

NSObject
// 当前线程上调用方法,可传参数....参数用来干啥? - (nullable id)performSelectorWithArgs:(SEL)sel, ...; // 当前线程延时几秒后调用,可传参数....参数用来干啥? - (void)performSelectorWithArgs:(SEL)sel afterDelay:(NSTimeInterval)delay, ...; // 延时调用方法,不可传参 - (void)performSelector:(SEL)sel afterDelay:(NSTimeInterval)delay; ...还有一些调用属性关于线程....// 当前类名 + (NSString *)className; ....NSObjectForKVO一些属性自行了解....

NSObject+YYModel
// 将`NSDictionary`,`NSString`,`NSData` json格式化 + (nullable instancetype)modelWithJSON:(id)json; //将一个数据字典转模型 + (nullable instancetype)modelWithDictionary:(NSDictionary *)dictionary; // 将json数据,转为model - (nullable id)modelToJSONObject; // 将model转data数据 - (nullable NSData *)modelToJSONData; // 将model转为字符串 - (nullable NSString *)modelToJSONString; // 将模型复制一份 - (nullable id)modelCopy; // 当前模型对象与另一个模型对象是否相等; YES,相等; 反之 - (BOOL)modelIsEqual:(id)model; // 模型的描素,返回一个字符串 - (NSString *)modelDescription;

NSArray (YYModel)
// 将json信息转为数组格式; 例如:[{"name","Mary"},{name:"Joe"}]; `cls `:[NSArray class] + (nullable NSArray *)modelArrayWithClass:(Class)cls json:(id)json; // 同上; + (nullable NSDictionary *)modelDictionaryWithClass:(Class)cls json:(id)json; // 替换属性,比如:id替换成ID格式如下: + (NSDictionary *)modelCustomPropertyMapper { return @{@"ID": @"id", } + (nullable NSDictionary *)modelCustomPropertyMapper; // 格式验证; 比如: @property NSString *name; + (NSDictionary *)modelContainerPropertyGenericClass{ return @{@"name" : [NSString class], } // 用于判断json数据格式的验证,保证数据正确性.->容器类型 + (nullable NSDictionary *)modelContainerPropertyGenericClass; // 模型解析时,黑白名单一下为黑白名单简介 + (nullable NSArray *)modelPropertyBlacklist; + (nullable NSArray *)modelPropertyWhitelist; // 如果一个 Model 需要忽略某些属性,则可以通过 实现协议中的 modelPropertyBlacklist 来返回属性名列表, YYModel 会在处理过程中忽略这些属性; // 如果一个 Model 只需要处理某些特性的属性,则可以通过实现 协议中的 modelPropertyWhitelist 来返回属性名列表, YYModel 在处理中只会处理列表内的属性

NSDate
// 返回当前时间,格式自己设置; 比如:[NSDate stringWithFormat:@"yyyy-MM-dd HH:mm:ss"]; - (nullable NSString *)stringWithFormat:(NSString *)format; // 将时间字符串以指定时间格式转换为Date + (nullable NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format;

NSTimer
// 和系统方法无差别,将SEL事件以block封装 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(void (^)(NSTimer *timer))block repeats:(BOOL)repeats; + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(void (^)(NSTimer *timer))block repeats:(BOOL)repeats;

UIApplication
/// "Documents" folder in this app's sandbox. @property (nonatomic, readonly) NSURL *documentsURL; @property (nonatomic, readonly) NSString *documentsPath; /// "Caches" folder in this app's sandbox. @property (nonatomic, readonly) NSURL *cachesURL; @property (nonatomic, readonly) NSString *cachesPath; /// "Library" folder in this app's sandbox. @property (nonatomic, readonly) NSURL *libraryURL; @property (nonatomic, readonly) NSString *libraryPath; /// Application's Bundle Name (show in SpringBoard). @property (nullable, nonatomic, readonly) NSString *appBundleName; /// Application's Bundle ID.e.g. "com.ibireme.MyApp" @property (nullable, nonatomic, readonly) NSString *appBundleID; /// Application's Version.e.g. "1.2.0" @property (nullable, nonatomic, readonly) NSString *appVersion; /// Application's Build number. e.g. "123" @property (nullable, nonatomic, readonly) NSString *appBuildVersion; /// Whether this app is being debugged (debugger attached). @property (nonatomic, readonly) BOOL isBeingDebugged; // 网络请求菊花旋转 - (void)incrementNetworkActivityCount; // 网络请求取消菊花旋转 - (void)decrementNetworkActivityCount;

UIDevice
//以下方法或属性的调用,都是基于[UIDevice currentDevice] 去实现的宏定义: // 当前设备系统版本号 #define kSystemVersion [UIDevice systemVersion] // 如果系统版本大于某系统号 #define kiOS8Later (kSystemVersion >= 8)// 当前设备的系统版本号,用了`dispatch_once_t`保证安全 + (double)systemVersion; /// Whether the device is iPad/iPad mini. @property (nonatomic, readonly) BOOL isPad; /// Whether the device is a simulator. @property (nonatomic, readonly) BOOL isSimulator; /// Whether the device is jailbroken(越狱). @property (nonatomic, readonly) BOOL isJailbroken; /// Wherher the device can make phone calls. @property (nonatomic, readonly) BOOL canMakePhoneCalls NS_EXTENSION_UNAVAILABLE_IOS(""); // 手机名字 e.g "iPhone 5s" "iPad mini 2" @property (nullable, nonatomic, readonly) NSString *machineModelName; /// The System's startup time.(启动时间) @property (nonatomic, readonly) NSDate *systemUptime; /// WIFI IP address of this device (can be nil). e.g. @"192.168.1.111" @property (nullable, nonatomic, readonly) NSString *ipAddressWIFI; /// Cell IP address of this device (can be nil). e.g. @"10.2.2.222" @property (nullable, nonatomic, readonly) NSString *ipAddressCell; ... 还有硬盘,内存使用情况等...

UIButton
// 设置button网络图片 - (void)setImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state placeholder:(nullable UIImage *)placeholder; // 设置button网络图, options 加载过程中的操作 - (void)setImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state options:(YYWebImageOptions)options; // 加载网络图片,button图片加载完成后,执行block操作 - (void)setImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options completion:(nullable YYWebImageCompletionBlock)completion; // 加载网络背景图片 - (void)setBackgroundImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state placeholder:(nullable UIImage *)placeholder; ....背景图加载与image加载相同,方法不同....

UIColor
// 16进制转成色值 e.g 0x66CCFF,默认1.0 alpha + (UIColor *)colorWithRGB:(uint32_t)rgbaValue; // 16进制转色值,可以设置透明度 + (UIColor *)colorWithRGB:(uint32_t)rgbValue alpha:(CGFloat)alpha; // 将一个颜色,转为16进制的值 - (uint32_t)rgbValue; ....获取 红,绿,蓝,饱和度,亮度,色调属性....

UIControl
// 移除当前控制器所有taget事件 - (void)removeAllTargets; // 增加target事件,这个是增加前把之前增加的移除 - (void)setTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; // 增加事件,block回调,注意循环引用 - (void)addBlockForControlEvents:(UIControlEvents)controlEvents block:(void (^)(id sender))block; // 增加事件,把以前增加都都移除掉 - (void)setBlockForControlEvents:(UIControlEvents)controlEvents block:(void (^)(id sender))block;

UIImage
// 根据Data加载GIF图片,如果图片较大,不能使用这个 + (nullable UIImage *)imageWithSmallGIFData:(NSData *)data scale:(CGFloat)scale; // 判断数据data是否可用于GIF动画 + (BOOL)isAnimatedGIFData:(NSData *)data; // 判断路径是否可用于GIF动画 + (BOOL)isAnimatedGIFFile:(NSString *)path; // 可传data和nsstring PDF文件转为image + (nullable UIImage *)imageWithPDF:(id)dataOrPath; // 将color转为图片,宽高1:1 + (nullable UIImage *)imageWithColor:(UIColor *)color; // 将color转为图片,size 自己写 + (nullable UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size; // 这个图像是否有alpha通道 - (BOOL)hasAlphaChannel; // 将图片裁剪,画圆角 无边框,无边框颜色 - (nullable UIImage *)imageByRoundCornerRadius:(CGFloat)radius; // 圆角,边框宽度,边框颜色 - (nullable UIImage *)imageByRoundCornerRadius:(CGFloat)radius borderWidth:(CGFloat)borderWidth borderColor:(nullable UIColor *)borderColor; // 圆角,边框宽度,边框颜色,边界线 - (nullable UIImage *)imageByRoundCornerRadius:(CGFloat)radius corners:(UIRectCorner)corners borderWidth:(CGFloat)borderWidth borderColor:(nullable UIColor *)borderColor borderLineJoin:(CGLineJoin)borderLineJoin; .... 图片的旋转,平移,纯色图片的生成....

UIImageView
// 图片链接,默认加载图 - (void)setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder; // 加载图片是可设置状态,e.g 状态栏菊花.图片渐现,使用缓存等... - (void)setImageWithURL:(nullable NSURL *)imageURL options:(YYWebImageOptions)options; //`YYWebImageCompletionBlock`包括:获取的image,url,YYWebImageFromType图片获取类型:内存获取,磁盘获取等,`YYWebImageStage`图片标志:取消,失败,完成等, - (void)setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options completion:(nullable YYWebImageCompletionBlock)completion; ....还有几个操作,此处省略....// 取消图片请求 - (void)cancelCurrentImageRequest; // 将请求完的图片设置为高亮,用法跟setimge一样. - (void)setHighlightedImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder; .... 高亮用法省略....

UIGestureRecognizer
// 手势触发事件,原来手势是通过SEL来实现事件,YYKit将其封装到block中 // 初始化手势时,增加block - (instancetype)initWithActionBlock:(void (^)(id sender))block; // 为某个手势增加事件 - (void)addActionBlock:(void (^)(id sender))block; // 移除手势所有的block - (void)removeAllActionBlocks;

UIScrollView
// 滚动到指定方向,默认有动画 - (void)scrollToTop; - (void)scrollToBottom; - (void)scrollToLeft; - (void)scrollToRight; // 滚动方向为指定方向,有无动画,传BOOL - (void)scrollToTopAnimated:(BOOL)animated; ```

UITableView
// 在做 TableView 的删除,移动等操作,需要在动画块中做操作, 要不然会造成崩溃问题 - (void)updateWithBlock:(void (^)(UITableView *tableView))block; // 滚动到指定行, - (void)scrollToRow:(NSUInteger)row inSection:(NSUInteger)section atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; // 插入到指定行 - (void)insertRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation; // 刷新某行 - (void)reloadRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation; // 删除某行 - (void)deleteRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation; // 通过 NSIndexPath 去插入 - (void)insertRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation; // 通过 NSIndexPath 刷新 - (void)reloadRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation; // 插入分区 - (void)insertSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation; ....刷新分区,删除分区,通过 path 刷新,删除,等....// 取消 tableview 中所有被选中的 cell - (void)clearSelectedRowsAnimated:(BOOL)animated;

YYAnimatedImageView
// YYAnimatedImageView 是继承与 UIImageView 的所以, UIImageView中加载图片方法都使用与它.如果做动画视图的话,用它比较合适.// 是否自动播放动画 @property (nonatomic) BOOL autoPlayAnimatedImage; // 当前动画图片有多少帧 @property (nonatomic) NSUInteger currentAnimatedImageIndex; // 判断当前图片是否正在播放动画 @property (nonatomic, readonly) BOOL currentIsPlayingAnimation;

CALayer
// 对 layer 进行图片 - (void)setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder; // 图层加载 - (void)setImageWithURL:(nullable NSURL *)imageURL options:(YYWebImageOptions)options; ...和 UIImage 用法差不多...

NSKeyedUnarchiver
// 对文件和数据进行解档,与系统方法无异,多了对异常的返回在 block 中// 对 data 数据进行解档 + (nullable id)unarchiveObjectWithData:(NSData *)data exception:(NSException *_Nullable *_Nullable)exception; // 对指定路径文件进行解档 + (nullable id)unarchiveObjectWithFile:(NSString *)path exception:(NSException *_Nullable *_Nullable)exception; YYImage && YYAnimatedImageView //YYImage *image = [YYImage imageNamed:@"animation.webp"]; YYAnimatedImageView *imageView = [YYAnimatedImageView alloc] initWithImage:image]; + (nullable YYImage *)imageNamed:(NSString *)name; // no cache! + (nullable YYImage *)imageWithContentsOfFile:(NSString *)path; + (nullable YYImage *)imageWithData:(NSData *)data; + (nullable YYImage *)imageWithData:(NSData *)data scale:(CGFloat)scale;

YYLabel
// YYLable 是继承于 UIView的类,用法与 Label 用法,属性无差别.// 设置文字阴影,设置的同时`attributedText`属性也被设置 @property (nullable, nonatomic, strong) UIColor *shadowColor; // 设置阴影的size 属性,设置的同时`attributedText`属性也被设置 @property (nonatomic) CGSize shadowOffset; // 阴影半径 @property (nonatomic) CGFloat shadowBlurRadius; // 文字垂直方向,枚举设置,三个方向 @property (nonatomic) YYTextVerticalAlignment textVerticalAlignment; ...还有一些属性是关于:`YYTextParser`,`YYTextLayout`,`YYTextLinePositionModifier`,`YYTextDebugOption`,它们本身是一个类,又当做 YYLabel 的属性,属于高级用法,自行了解...// 文字的点击事件 @property (nullable, nonatomic, copy) YYTextAction textTapAction;

YYTextView
// YYTextView 的代理,属性与系统的用法一样.// 如果文字中有网址链接,设置链接样式,默认为 None 点击事件由代理实现 @property (nonatomic) UIDataDetectorTypes dataDetectorTypes; // 当“dataDetectorTypes”检测到一系列文本时,该值将是 用于修改范围内的原始属性 @property (nullable, nonatomic, copy) NSDictionary *linkTextAttributes; // 设置文字的富文本属性,更多设置看`NSAttributedString+YYText` @property (nullable, nonatomic, copy) NSAttributedString *attributedText; // 可以查询 layout 一些信息,只读 @property (nullable, nonatomic, strong, readonly) YYTextLayout *textLayout; // 类似于 UITextFile 属性,当没有文字输入时,显示 @property (nullable, nonatomic, copy) NSString *placeholderText; ...还有一个颜色, font placeholder 富文本属性...// text 的 inset 属性 @property (nonatomic) UIEdgeInsets textContainerInset; // 用户是否可以对内容进行选择 @property (nonatomic, getter=isSelectable) BOOL selectable; // 对内容是否可以编辑 @property (nonatomic, getter=isEditable) BOOL editable; // 是否允许用户将图片粘贴复制到文本框,默认为 NO @property (nonatomic) BOOL allowsPasteImage; // 是否允许将其他富文本属性粘贴至文本框,默认为 NO @property (nonatomic) BOOL allowsPasteAttributedString; // 是否允许将文本框中的富文本字符串进行拷贝,默认为 YES @property (nonatomic) BOOL allowsCopyAttributedString; // 文本框是否有,撤销,等状态,默认为 YES @property (nonatomic) BOOL allowsUndoAndRedo;

YYTimer
// 在指定时间开启定时器 - (instancetype)initWithFireTime:(NSTimeInterval)start interval:(NSTimeInterval)interval target:(id)target selector:(SEL)selector repeats:(BOOL)repeats NS_DESIGNATED_INITIALIZER;

原文链接

    推荐阅读