I'm making chatting App on iPhone. I have a problem with sizeWithFont:constrainedToSize:lineBreakMode function. When NSString has a long length white space, sizeWithFont() always return (width:218, height:18)
This is my test code.
NSMutableString *string = [[NSMutableString alloc]initWithString:@""];
for(NSInteger i = 0; i < 99; i++) {
[string insertString:@" " atIndex:0];
NSInteger length = string.length;
CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]] constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"Text:%@\nlength: %d\nsize: %f, %f\n",string, length, size.width, size.height, nil);
}
And this is result.
Text:
length: 1
size: 4.000000, 18.000000
Text:
length: 2
size: 8.000000, 18.000000
Text:
length: 3
size: 12.000000, 18.000000
.
.
.
Text:
length: 56
size: 218.000000, 18.000000
Text:
length: 57
size: 218.000000, 18.000000
Text:
length: 58
.
.
.
Text:
length: 99
size: 218.000000, 18.000000
Question: How can I get the right size of long white space string?
It's probably because you are using
NSLineBreakByWordWrapping
and there are no words.Try using
NSLineBreakByCharWrapping
.