finding the length of an attributed string in ios 6 and above

[sourcecode lang=”objc”]
– (CGFloat)widthOfString:(NSString *)string withFont:(UIFont *)font {
  NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, kCTFontAttributeName, nil]; 
  return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width;
}
[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *