Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (NSArray *)splitString:(NSString*)str maxWidth:(CGFloat)width forFont:(UIFont *)font {
- NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
- NSArray *wordArray = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- NSString *line = @"";
- NSString *lineWithNext = @"";
- for (int i = 0; i < [wordArray count]; i++) {
- if (line.length == 0) { line = [wordArray objectAtIndex:i]; }
- if (i+1 < [wordArray count]) {
- lineWithNext = [[NSArray arrayWithObjects:line, [wordArray objectAtIndex:i+1], nil] componentsJoinedByString:@" "];
- if (((int)[lineWithNext sizeWithFont:font].width) <= width) {
- line = lineWithNext;
- } else {
- [tempArray addObject:line];
- line = @"";
- }
- } else {
- [tempArray addObject:line];
- }
- }
- return tempArray;
- }
Add Comment
Please, Sign In to add comment