NSString : easy way to remove UTF-8 accents from a string?

NSString *str = @”Être ou ne pas être. C’était là-bas.”; NSData *data = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *newStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@”%@”, newStr); … or try using NSUTF8StringEncoding instead. List of encoding types here: https://developer.apple.com/documentation/foundation/nsstringencoding Just FTR here’s a one line way to write this great answer: yourString = [[NSString alloc] initWithData: [yourString … Read more

How to get substring of NSString?

Option 1: NSString *haystack = @”value:hello World:value”; NSString *haystackPrefix = @”value:”; NSString *haystackSuffix = @”:value”; NSRange needleRange = NSMakeRange(haystackPrefix.length, haystack.length – haystackPrefix.length – haystackSuffix.length); NSString *needle = [haystack substringWithRange:needleRange]; NSLog(@”needle: %@”, needle); // -> “hello World” Option 2: NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@”^value:(.+?):value$” options:0 error:nil]; NSTextCheckingResult *match = [regex firstMatchInString:haystack options:NSAnchoredSearch range:NSMakeRange(0, haystack.length)]; NSRange needleRange … Read more

Using NSRegularExpression to extract URLs on the iPhone

The method matchesInString:options:range: returns an array of NSTextCheckingResult objects. You can use fast enumeration to iterate through the array, pull out the substring of each match from your original string, and add the substring to a new array. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@”http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?” options:NSRegularExpressionCaseInsensitive error:&error]; NSArray *arrayOfAllMatches = [regex matchesInString:httpLine options:0 range:NSMakeRange(0, [httpLine length])]; NSMutableArray … Read more

How to create subscript characters that’s not in Unicode in iOS

I wasn’t able to get NSSuperscriptAttributeName to work but had success with the following: UILabel *label = [[UILabel alloc] init]; NSString *string = @”abcdefghi”; NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string]; NSInteger num1 = 1; CFNumberRef num2 = CFNumberCreate(NULL, kCFNumberNSIntegerType, &num1); [attrString addAttribute:(id)kCTSuperscriptAttributeName value:(id)num2 range:NSMakeRange(4,2)]; label.attributedText = attrString; [attrString release]; This gives you: Assigning the attributed … Read more

Measuring the pixel width of a string

On iPhone OS it is slightly different, instead look at the NSString UIKit Additions Reference. The idea is the same as in Cocoa for Mac OS X, but there are more methods. For single lines of text use: – (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode And for multiline texts use: – (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode The use … Read more

NSString is integer?

You could use the -intValue or -integerValue methods. Returns zero if the string doesn’t start with an integer, which is a bit of a shame as zero is a valid value for an integer. A better option might be to use [NSScanner scanInt:] which returns a BOOL indicating whether or not it found a suitable … Read more