Guest User

Untitled

a guest
Dec 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. - (NSArray *)splitString:(NSString*)str maxWidth:(CGFloat)width forFont:(UIFont *)font {
  2. NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
  3. NSArray *wordArray = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  4. NSString *line = @"";
  5. NSString *lineWithNext = @"";
  6.  
  7. for (int i = 0; i < [wordArray count]; i++) {
  8. if (line.length == 0) { line = [wordArray objectAtIndex:i]; }
  9.  
  10. if (i+1 < [wordArray count]) {
  11. lineWithNext = [[NSArray arrayWithObjects:line, [wordArray objectAtIndex:i+1], nil] componentsJoinedByString:@" "];
  12. if (((int)[lineWithNext sizeWithFont:font].width) <= width) {
  13. line = lineWithNext;
  14. } else {
  15. [tempArray addObject:line];
  16. line = @"";
  17. }
  18. } else {
  19. [tempArray addObject:line];
  20. }
  21. }
  22. return tempArray;
  23. }
Add Comment
Please, Sign In to add comment