Introduction To Regular Expressions In Objective-C: Search String For Email Addresses

Regular expressions (aka regex) are extremely powerful for string matching, if you are not familiar with how to use them, I highly recommend you take some time to learn more.

To give you a taste, below is a block of code that uses the NSRegularExpression and NSTextCheckingResult classes to create a compiled regular expression and extract content from the same.

The example will search for all email addresses numbers in the following string:

NSString *str = @"Sed ut perspiciatis, unde omnis iste
 natus error sit voluptatem accusantium doloremque laudantium,
 totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto
 beatae vitae dicta sunt, explicabo.";

The output we are after:

Continue reading

Convert Hex Value Stored As NSString To Integer

If you have a hex value that is stored in an NSString object and need to convert the value into an integer, the NSScanner object can be of great help.

Let’s say for example that you need convert the string “0xABCD” to its integer equivalent. Or maybe you are working with values that are related to a color selection, then you may have a value in hex that looks like this “#12BC99.” In either case, the method below will convert the NSString using NSScanner and return an integer.
Continue reading

Remove Any Character Or Set Of Characters From String Using NSCharacterSet and NSMutableCharacterSet

Although the NSString class has methods such as rangeOfCharacterFromSet: and stringByReplacingOccurrencesOfString:withString:, often times you may need more flexibility than either of these two methods afford.

For example, if you need to remove all occurrences of any character in a set, NSCharacter and NSMutableCharacterSet are the way to go.

Let’s assume you wanted to remove all numbers from the string “99 bottles of beer on the wall”

NSString *str = @"99 bottles of beer on the wall";
NSLog(@"String: %@", str);
// Create character set with digits only
NSMutableCharacterSet *characterSet = [NSMutableCharacterSet decimalDigitCharacterSet];
// Build array of components using digits as separator
NSArray *arrayOfComponents = [str componentsSeparatedByCharactersInSet:characterSet];
// Create string from the array components
NSString *strOutput = [arrayOfComponents componentsJoinedByString:@""];
NSLog(@"New string: %@", strOutput);

The output will looks as follows:

String: 99 bottles of beer on the wall
New string: bottles of beer on the wall
Continue reading

Truncate String Adding Ellipsis At Front, Middle Or End

Using attributed strings you can easily truncate strings, adding ellipsis at the front, middle or end.

For example, let’s say you have the string: @”May you always have a cellar full of beer.” and want to display this within a label, having the string truncated as needed to fit. Using an NSMutableParagraphStyle and adding this to an attributed string you can create output that looks as follows:

Continue reading

iOS 6 NSAttributedString, Set Text Font, Foreground And Background Colors, StrikeThrough And Shadow – Part 1

Attributed strings make an association between characters and their attributes. Like NSString objects, there are two variations, NSAttributedString and NSMutableAttributedString.

Although previous versions of iOS supported attributed strings, it wasn’t until iOS 6 that controls such as buttons, labels, textfields and textviews defined a property to manage attributes.

Attributes are applied to a range of characters, so you can for example, set a strikethrough attribute for just a portion of a string. It’s also important to note that the default font for attributed string objects is Helvetica 12-point. Keep this in mind if you set the font attribute for a range other than the complete string.

The following attributes can be set with attributed strings:

NSString *const NSFontAttributeName;
NSString *const NSParagraphStyleAttributeName;
NSString *const NSForegroundColorAttributeName;
NSString *const NSBackgroundColorAttributeName;
NSString *const NSLigatureAttributeName;
NSString *const NSKernAttributeName;
NSString *const NSStrikethroughStyleAttributeName;
NSString *const NSUnderlineStyleAttributeName;
NSString *const NSStrokeColorAttributeName;
NSString *const NSStrokeWidthAttributeName;
NSString *const NSShadowAttributeName;
NSString *const NSVerticalGlyphFormAttributeName;
Continue reading