ABUtils 0.1.4

ABUtils 0.1.4

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Jan 2017

Maintained by Andrew Boryk.



ABUtils 0.1.4

  • By
  • Andrew Boryk

Description

ABUtils is a library which provides pre-written functionality. This makes it easier to develop cleaner code by removing the need for repetitive code, as well as easier to get started without having to re-write code between projects. As demand increases for a function, it will be added.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • Requires iOS 8.0 or later
  • Requires Automatic Reference Counting (ARC)

Installation

ABUtils is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "ABUtils"

Usage

Dev Functions

Print can be used in replacement for NSLog. Print to console is done through CFShow. Also, print to console will not be done on production, only will print when app is in Debug mode. Print has two methods:

  1. 'print:tag': Accepts an object to be printed to console, as well as a tag string to preceed it
  2. 'printString': Accepts a string and prints it to console
/// 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;

The following macros can be used to easily determine the iOS version of the user's device:

/// 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

Conditional Functions

The 'notNull' function is helpful in determining whether an object is valid, that is, the value is not null or nil. In Swift, you can determine if a variable is nil using '!'. This is not available in Objective-C, so this function can be used in its place. It has a sister-function, 'isNull', if you are looking to determine that a variable is null or nil.

/// 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;

Similar to the 'notNull' function, the 'notNil' is useful in determining whether an object is nil. In Swift, you can determine if a variable is nil using '!'. This is not available in Objective-C, so this function can be used in its place. It has a sister-function, 'isNil', if you are looking to determine that a variable is nil.

/// 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;

The 'notBlank' function is useful in determining whether a string is blank. This function can be used as a conditional in determining if a user has filled out a textfield.

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

To determine if an email is a valid format, you can use the 'isValidEmail' function. This function uses a regex to ensure that the email is in the proper format and only contains only valid email address characters.

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

The last of the conditional functions is 'boolValue'. This function is used to parse the bool value out of an NSString or NSNumber.

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

String Modification Functions

There are several functions to modifying and cleaning strings:

  1. 'removeSpecialCharacters': Removes characters from the string that are not an upper or lowercase letter
  2. 'trimWhiteSpace': Trims white space from the ends of a string, such as ' ' and '\n'
  3. 'trimMultiSpace': Trims multispace from a string, and turns '\n\n' into '\n' as well as ' ' into ' '
  4. 'trimWhiteAndMultiSpace': An option which combines the previous two functions
  5. 'removeSpaces': Removes all spaces and new lines from a string
/// 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;

Time Functions

The 'timeZone' function quickly returns the time zone of the user in string format

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

The 'ordinalSuffixFromInt' function returns the proper ordinal suffix for an int (ie. st, rd, nd). The 'ordinalNumberString' function simplifies the previous function, by supplying the ordinal suffix, and appending it to the number, returning a full string

/// 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;

The 'endOfDay' function returns an NSDate which represents 11:59 PM on the provided date. The 'endOfTomorrow' function is similar to the previous function, but provides the end of the date for tomorrow.

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

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

The 'differenceMet' function is used to determine if a number of specified days have passed between now and the date provided.

/// 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;

Number Functions

The following functions return a string formatted from a number:

  1. 'decimalNumber': Formats the number as a string with commas, accepting an NSNumber. This function should be called using a sharedInstance. Date and number formatters take up processing time, so this makes sure that the formatter used for this function is only initilized once.
  2. 'commaFormat': Uses the previous function, but accepts an int.
  3. 'commaFormatNumber': Formats the number to be a decimal with 1-3 number places, and shows K, M, B, or T (Thousand, Million, Billion, Trillion).
/// 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 Functions

'colorWithHexString' function is a very useful function. Currently, there is no easy way to use Hex strings to declare UIColors. This function accepts a 6-letter hex string (don't use the shortcut hex strings) and returns the UIColor for that string.

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

The following functions provide a 1 line means to determine the model of a device

  1. 'platformType': Returns the device and model of the user's device (ie. iPhone 5S)
  2. 'modelTypeString': There are currently 4 different resolution sizes. When developing apps and adjusting UI depending on the size of the devices screen, this function lets you know what the screen resolution is based off of specific models. The 5 types that can be returned: iPhone 3.5 inch, iPhone 4 inch, iPhone 6 size, iPhone Plus size, and Simulator. I chose names that I could understand consistently for reference.
  3. 'modelTypeSize': This function is the same as the previous, however it returns a type using my custom enum. The different values are - iPhone4Size, iPhone5Size, iPhone6Size, iPhone6PlusSize, and 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;

Image/Video Functions

The following functions provide tools in editing and encoding media:

  1. 'scaleAndRotateImage': Scales the image to 1080px and rotates it to the orientation it was taken in
  2. 'generateBlackAndWhiteImage': Generates a black and white version of the provided image
  3. 'generateSepiaImage': Generates a sepia version of the provided image
  4. 'generateSaturatedImage': Generates a saturated version of the provided image
  5. 'encodeToBase64String': Encodes the image provided into a Base64 string
  6. 'encodeVideoToBase64String': Encodes the video provided into a Base64 string
/// 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;

The function 'reorient' accepts an orientation change and a size, and returns the correct CGAffineTransform necessary to receive the result requested. This function proves very useful when working with AVAssetExporters and custom AVPlayers. Any person that has had to work with AVAssetExporter in order to crop videos will understand how useful this function can be in achieving the correct video orientation when the AVAsset is exported.

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

Author

Andrew Boryk, [email protected]

License

ABUtils is available under the MIT license. See the LICENSE file for more info.