Seems like there is no factory way of doing this, so have started constructing a class function to handle this, based on the solution introduced at this closely related stack :
+ (NSString*)wrappedString:(NSString*)string withFont:(UIFont*)font andWidth:(float)width {
NSMutableString *resultString = [[NSMutableString alloc] initWithString:@""];
CGSize textSize = [string sizeWithFont:font];
float textWidth = textSize.width;
if (textWidth < width) {
return string;
}
float wordLength;
float lineLength;
NSUInteger length = [string length];
unichar buffer[length];
[string getCharacters:buffer range:NSMakeRange(0, length)];
NSString *singleLine = @"";
NSString *word = @"";
NSString *longWord = @"";
for (NSUInteger i = 0; i < length; i++) {
unichar character = buffer[i];
if (character != '\n') {
word = [NSString stringWithFormat:@"%@%c", word, character];
}
if (character == '\n') {
float wordLength = [word sizeWithFont:font].width;
float lineLength = [singleLine sizeWithFont:font].width;
if ((lineLength + wordLength) > width) {
[resultString appendString:singleLine];
[resultString appendString:@"\n"];
singleLine = @"";
singleLine = [singleLine stringByAppendingFormat:@"%@\n",word];
word = @"";
} else {
singleLine = [singleLine stringByAppendingString: word];
word = @"";
[resultString appendString:singleLine];
[resultString appendString:@"\n"];
singleLine = @"";
}
}
else if (character == ' ') {
float wordLength = [word sizeWithFont:font].width;
float lineLength = [singleLine sizeWithFont:font].width;
if ((lineLength + wordLength) > width) {
if (wordLength > textWidth) {
[resultString appendString:singleLine];
[resultString appendString:@"\n"];
singleLine = @"";
int j = 0;
for (; j < [word length]; j++) {
unichar longChar = [word characterAtIndex:j];
longWord = [NSString stringWithFormat:@"%@%c", longWord, longChar];
float longwordLength = [longWord sizeWithFont:font].width;
float longlineLength = [singleLine sizeWithFont:font].width;
if ((longlineLength + longwordLength) >= width) {
singleLine = [singleLine stringByAppendingString:longWord];
word = @"";
longWord = @"";
break;
}
}
}
[resultString appendString:singleLine];
[resultString appendString:@"\n"];
singleLine = @"";
}
singleLine = [singleLine stringByAppendingString: word];
word = @"";
}
}
wordLength = [word sizeWithFont:font].width;
lineLength = [singleLine sizeWithFont:font].width;
if (wordLength > 0) {
if ((lineLength + wordLength) > width) {
[resultString appendString:singleLine];
[resultString appendString:@"\n"];
singleLine = @"";
}
singleLine = [singleLine stringByAppendingString:word];
}
if (lineLength > 0) {
[resultString appendString:singleLine];
[resultString appendString:@"\n"];
}
return resultString;
}