With what should I replace the deprecated sizeWithFont: method?

After an hour of trial error I managed to make it work: CGSize maximumLabelSize = CGSizeMake(tableView.width, MAXFLOAT); NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin; NSDictionary *attr = @{NSFontAttributeName: [UIFont systemFontOfSize:15]}; CGRect labelBounds = [string boundingRectWithSize:maximumLabelSize options:options attributes:attr context:nil]; Update: As Mr. T mentions in answer below : In iOS 7 and later, this method returns fractional … Read more

Find all locations of substring in NSString (not just first)

You can use rangeOfString:options:range: and set the third argument to be beyond the range of the first occurrence. For example, you can do something like this: NSRange searchRange = NSMakeRange(0,string.length); NSRange foundRange; while (searchRange.location < string.length) { searchRange.length = string.length-searchRange.location; foundRange = [string rangeOfString:substring options:0 range:searchRange]; if (foundRange.location != NSNotFound) { // found an occurrence … 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