How to use drawInRect:withAttributes: instead of drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment: in iOS 7

You can use NSDictionary and apply attributes like this: NSFont *font = [NSFont fontWithName:@”Palatino-Roman” size:14.0]; NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys: font, NSFontAttributeName, [NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil]; Use attrsDictionary as argument. Refer: Attributed String Programming Guide Refer: Standard Attributes SWIFT: IN String drawInRect is not available but we can use NSString instead: let font = UIFont(name: …

Read more

Objective-C: Find numbers in string

Here’s an NSScanner based solution: // Input NSString *originalString = @”This is my string. #1234″; // Intermediate NSString *numberString; NSScanner *scanner = [NSScanner scannerWithString:originalString]; NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@”0123456789″]; // Throw away characters before the first number. [scanner scanUpToCharactersFromSet:numbers intoString:NULL]; // Collect numbers. [scanner scanCharactersFromSet:numbers intoString:&numberString]; // Result. int number = [numberString integerValue]; (Some of …

Read more

Search through NSString using Regular Expression

You need to use NSRegularExpression class. Example inspired in the documentation: NSString *yourString = @””; NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@”(NS|UI)+(\\w+)” options:NSRegularExpressionCaseInsensitive error:&error]; [regex enumerateMatchesInString:yourString options:0 range:NSMakeRange(0, [yourString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){ // your code to handle matches here }];

NSData to display as a string

Use the NSString initWithData:encoding: method. NSString *someString = [[NSString alloc] initWithData:hashedData encoding:NSASCIIStringEncoding]; (edit to respond to your comment:) In that case, Joshua’s answer does help: NSCharacterSet *charsToRemove = [NSCharacterSet characterSetWithCharactersInString:@”< >”]; NSString *someString = [[hashedData description] stringByTrimmingCharactersInSet:charsToRemove];

How to make an NSString path (file name) safe

This will remove all invalid characters anywhere in the filename based on Ismail’s invalid character set (I have not verified how complete his set is). – (NSString *)_sanitizeFileNameString:(NSString *)fileName { NSCharacterSet* illegalFileNameCharacters = [NSCharacterSet characterSetWithCharactersInString:@”/\\?%*|\”<>”]; return [[fileName componentsSeparatedByCharactersInSet:illegalFileNameCharacters] componentsJoinedByString:@””]; } Credit goes to Peter N Lewis for the idea to use componentsSeparatedByCharactersInSet: NSString – Convert …

Read more