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

align text using drawInRect:withAttributes:

There is one key to set the paragraph style of the text (including line breaking mode, text alignment, and more). From docs: NSParagraphStyleAttributeName The value of this attribute is an NSParagraphStyle object. Use this attribute to apply multiple attributes to a range of text. If you do not specify this attribute, the string uses the … Read more

Replacing one character in a string in Objective-C

If it is always the same character you can use: stringByReplacingOccurrencesOfString:withString: If it is the same string in the same location you can use: stringByReplacingOccurrencesOfString:withString:options:range: If is just a specific location you can use: stringByReplacingCharactersInRange:withString: Documentation here: https://developer.apple.com/documentation/foundation/nsstring So for example: NSString *someText = @”Goat”; NSRange range = NSMakeRange(0,1); NSString *newText = [someText stringByReplacingCharactersInRange:range withString:@”B”]; … Read more