Created
October 29, 2012 07:38
-
-
Save MosheBerman/3972164 to your computer and use it in GitHub Desktop.
Return a substring that fits a given rect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSString+MBDialogString.m | |
// TileParser | |
// | |
// Created by Moshe Berman on 9/23/12. | |
// | |
// | |
#import "NSString+MBDialogString.h" | |
@implementation NSString (MBDialogString) | |
- (NSString *) substringThatFitsFrame:(CGRect)frame withFont:(UIFont *)font{ | |
NSString *truncatedString = self; | |
CGSize size = [truncatedString sizeWithFont:font constrainedToSize:CGSizeMake(frame.size.width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByTruncatingTail]; | |
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:frame.size lineBreakMode:NSLineBreakByWordWrapping]; | |
} | |
return truncatedString; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment