ABUtils 0.1.4

ABUtils 0.1.4

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布上次发布2017年1月

Andrew Boryk 维护。



ABUtils 0.1.4

  • Andrew Boryk

描述

ABUtils 是一个提供预编写功能的库。通过减少重复代码的需要,它使编写更干净的代码变得更容易,同时更容易开始项目而无需重写代码。随着功能需求的增加,功能将被添加。

示例

要运行示例项目,首先克隆仓库,然后从示例目录运行 pod install

要求

  • 需要 iOS 8.0 或更高版本
  • 需要自动引用计数(ARC)

安装

ABUtils 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行

pod "ABUtils"

使用

开发功能

Print 可以用来替代 NSLog。通过 CFShow 到控制台打印。同时,在产品中不会进行打印,只有在调试模式下应用时才会打印。Print 有两种方法

  1. 'print:tag': 接受一个要打印到控制台的对象,以及一个前置标签字符串
  2. 'printString': 接受一个字符串并将其打印到控制台
/// Print object to console using CFShow, includes a tag which will preceed the object in the print (ie. "Tag: Object") 
+ (void) print: (id) object tag: (NSString *) tag;

/// Print a string to console using CFShow
+ (void) printString: (NSString *) string;

以下宏可以用来轻松确定用户设备的 iOS 版本

/// Returns true if the device's iOS is version 5.0 or later
IS_OS_5_OR_LATER 

/// Returns true if the device's iOS is version 6.0 or later
IS_OS_6_OR_LATER

/// Returns true if the device's iOS is version 7.0 or later
IS_OS_7_OR_LATER

/// Returns true if the device's iOS is version 8.0 or later
IS_OS_8_OR_LATER

/// Returns true if the device's iOS is version 9.0 or later
IS_OS_9_OR_LATER

/// Returns true if the device's iOS is version 10.0 or later
IS_OS_10_OR_LATER

条件函数

'notNull' 函数有助于确定一个对象是否有效,即其值不是null或nil。在 Swift 中,您可以使用 '!' 确定变量是否为 nil。这在 Objective-C 中不可用,因此可以使用此函数替代。如果您想确定变量是否为 null 或 nil,它有一个姊妹函数 'isNull'

/// Returns true if the object is not null or nil, otherwise returns false
+ (BOOL)notNull:(id)object;

/// Returns true if the object is null or nil, otherwise returns false
+ (BOOL)isNull:(id)object;

'notNull' 函数类似,'notNil' 在确定对象是否为 nil 时很有用。在 Swift 中,您可以使用 '!' 来确定变量是否为 nil。这在 Objective-C 中不可用,因此可以使用此函数替代。如果您想确定变量是否为 nil,它有一个姊妹函数 'isNil'

/// Returns true if the object is not nil, returns true if the object is null
+ (BOOL)notNil:(id)object;

/// Returns true if the object is nil, returns false if the object is null
+ (BOOL)isNil:(id)object;

《notBlank》函数用于判断字符串是否为空。此函数可用于判断用户是否填写了文本字段。

/// Returns true if the object is not just spaces or blank, otherwise returns false
+ (BOOL)notBlank: (NSString *) text;

要确定电子邮件地址是否有效格式,可以使用《isValidEmail》函数。此函数使用正则表达式来确保电子邮件地址格式正确,并且只包含有效的电子邮件地址字符。

/// Determines if email is valid format
+ (BOOL)isValidEmail: (NSString *)string;

最后的条件函数是《boolValue》。此函数用于从NSString或NSNumber中解析出布尔值。

/// Determines bool value for a NSString or NSNumber
+ (BOOL) boolValue: (id) value;

字符串修改函数

有几个函数用于修改和清理字符串

  1. ‘removeSpecialCharacters’:从字符串中删除不是大写或小写字母的字符
  2. ‘trimWhiteSpace’:从字符串两端剪除空白字符,例如‘ ‘和‘\n’
  3. ‘trimMultiSpace’:从字符串中剪除多空格,并将‘\n\n’转换为‘\n’,以及将‘ ‘转换为‘ ’
  4. ‘trimWhiteAndMultiSpace’:结合上述前两个函数的选项
  5. ‘removeSpaces’:从字符串中删除所有空格和新行
/// Removes characters from the string that are not an upper or lowercase letter
+ (NSString *)removeSpecialCharacters: (NSString *) text;

/// Trims white space from the ends of a string, such as ' ' and '\n'
+ (NSString *)trimWhiteSpace: (NSString *) text;

/// Trims multispace from a string, and turns '\n\n' into '\n' as well as '  ' into ' '
+ (NSString *)trimMultiSpace: (NSString *) text;

/// Trims white space and removes extra new lines from string, and replaces instances of "\n\n" with "\n" and "  " with " "
+ (NSString *)trimWhiteAndMultiSpace: (NSString *) text;

/// Removes all spaces and new lines from a string
+ (NSString *)removeSpaces: (NSString *) text;

时间函数

《timeZone》函数可以快速返回用户时区,并以字符串格式返回

/// Determines the timezone of the user, and returns a string value of that time zone
+ (NSString *)timeZone;

《ordinalSuffixFromInt》函数返回整数的正确序列后缀(如st, rd, nd)。《ordinalNumberString》函数通过提供序列后缀并将其附加到数字上,简化了上述函数,从而返回完整的字符串

/// Returns the ordinal suffix for a number (ie. th, st, nd)
+ (NSString *)ordinalSuffixFromInt:(NSInteger)number;

/// Returns the number in string format with its proper ordinal suffix (ie. 5th, 1st, 2nd)
+ (NSString *)ordinalNumberString:(NSInteger)number;

《endOfDay》函数返回一个表示提供日期晚上11:59的NSDate。《endOfTomorrow》函数与上一个函数类似,但提供的是明天的日期结束时间。

// Returns the NSDate for the end of date received
+ (NSDate *)endOfDay:(NSDate *)date;

// Returns the NSDate for the end of tomorrow
+ (NSDate *)endOfTomorrow;

《differenceMet》函数用于判断从现在到提供的日期之间是否已过去指定天数。

/// Determines if the time difference between now and the date received is more or equal to the number of days received
+ (BOOL)differenceMet: (NSDate *) time days: (int) days;

数字函数

以下函数从数字返回字符串格式

  1. ‘decimalNumber’:以带有逗号的字符串格式化数字,接受NSNumber。此函数应使用sharedInstance调用。日期和数字格式化会消耗处理时间,因此确保用于此函数的格式化器仅初始化一次。
  2. ‘commaFormat’:使用前一个函数,但接受整数
  3. ‘commaFormatNumber’:将数字格式化为1-3位小数,并显示K、M、B或T(千、百万、十亿、万亿)。
/// Returns decimal string for the number, with commas
- (NSString *) decimalNumber: (NSNumber *)value;

/// Returns int in comma format
+ (NSString *) commaFormat: (int)value;

/// Accepts number and returns it in comma format
+ (NSString *) commaFormatNumber: (NSNumber *)value;

UI函数

《colorWithHexString》函数是一个非常实用的函数。目前,还没有简单的方法可以使用十六进制字符串来声明UIColor。此函数接受6位十六进制字符串(不要使用快捷十六进制字符串)并返回相应的UIColor。

/// Returns a UIColor for a hex string value
+ (UIColor *)colorWithHexString:(NSString*)hex;

以下函数提供一种简单的方法来确定设备的型号

  1. ‘platformType’:返回用户设备的设备和型号(例如iPhone 5S)
  2. ‘modelTypeString’:目前有4种不同的分辨率尺寸。在开发应用程序并调整依赖于设备屏幕大小的UI时,此函数告知您基于特定型号的屏幕分辨率。可返回的5种类型:3.5英寸iPhone、4英寸iPhone、iPhone 6尺寸、iPhone Plus尺寸和模拟器。我选择了可以持续理解参考的名称。
  3. ‘modelTypeSize’: 这个函数与之前的函数相同,但是它返回一个使用我自定义枚举的类型。不同的值包括 - iPhone4Size、iPhone5Size、iPhone6Size、iPhone6PlusSize 和 Simulator。
/// Returns the platform type of the user's phone (ie. "iPhone 5S")
+ (NSString *)platformType;

/// Returns the screen size of the device (iPhone only) (ie. "iPhone Plus size")
+ (NSString *)modelTypeString;

/// Determines model type for screen size (ie. iPhone4Size)
+ (ModelSizeType)modelTypeSize;

图像/视频函数

以下函数提供编辑和编码媒体的工具

  1. ‘scaleAndRotateImage’: 将图像缩放到 1080 像素,并将其旋转到拍照时的方向
  2. ‘generateBlackAndWhiteImage’: 生成提供图像的黑白版本
  3. ‘generateSepiaImage’: 生成提供图像的棕褐色版本
  4. ‘generateSaturatedImage’: 生成提供图像的饱和版本
  5. ‘encodeToBase64String’: 将提供的图像编码为 Base64 字符串
  6. ‘encodeVideoToBase64String’: 将提供的视频编码为 Base64 字符串
/// Send image, and returns same image scaled to 1080px and rotated accordingly
+ (UIImage *)scaleAndRotateImage:(UIImage *)image;

/// Generated a black and white image from the image received
+ (UIImage *) generateBlackAndWhiteImage: (UIImage *) image;

/// Generates an image with sepia tone from the image receieved
+ (UIImage *) generateSepiaImage: (UIImage *) image;

/// Generates a saturated image from the image received
+ (UIImage *) generateSaturatedImage: (UIImage *) image;

/// Encodes image to Base 64 for Backend
+ (NSString *)encodeToBase64String:(UIImage *)image;

/// Encodes video to Base 64 for Backend
+ (NSString *)encodeVideoToBase64String:(NSURL *)videoURL;

‘reorient’: 接受一个方向改变和大小,并返回必要的 CGAffineTransform 来接收请求的结果。当与 AVAssetExporters 和自定义 AVPlayers 一起工作时,这个函数非常有用。任何需要使用 AVAssetExporter 来裁剪视频的人都会了解这个函数在导出 AVAsset 时达到正确的视频方向有多么有用。

/// Provides the orientation transformation for reorienting videos
+ (CGAffineTransform) reorient: (OrientationType)orientation size: (CGSize)size;

作者

Andrew Boryk,[email protected]

许可证

ABUtils 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。