Skip to content

Instantly share code, notes, and snippets.

@MosheBerman
Created October 29, 2012 08:31
Show Gist options
  • Save MosheBerman/3972372 to your computer and use it in GitHub Desktop.
Save MosheBerman/3972372 to your computer and use it in GitHub Desktop.
Working text to dialog array
//
// Calculates the substring that fits in a frame
//
- (NSString *) substringThatFitsFrame:(CGRect)frame withFont:(UIFont *)font{
NSString *truncatedString = self;
CGSize clippingSize = CGSizeMake(frame.size.width, CGFLOAT_MAX);
CGSize size = [truncatedString sizeWithFont:font constrainedToSize:clippingSize lineBreakMode:NSLineBreakByClipping];
NSMutableArray *components = [[truncatedString componentsSeparatedByString:@" "] mutableCopy];
while (frame.size.width <= size.width || frame.size.height <= size.height) {
[components removeLastObject];
truncatedString = [components componentsJoinedByString:@" "];
size = [truncatedString sizeWithFont:font constrainedToSize:clippingSize lineBreakMode:NSLineBreakByClipping];
}
return truncatedString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment