iphone:sizeWithFont方法不会从字符串中识别'\' [英] iphone : sizeWithFont method does not identify the '\n' from string
问题描述
NSString * str = @这是\\\
\\\
\\\
;
CGSize size = [str sizeWithFont:[UIFont fontWithName:@Verdanasize:12.5] forWidth:300
lineBreakMode:UILineBreakModeWordWrap];
NSLog(@Height:%f Width:%f,size.height,size.width);
但是,id并不考虑字符串中的\\\
个字符来更改行。当我在标签上显示此字符串并将其numberOfLines属性设置为大约4或5时,标签会考虑\\\
并转到下一行。考虑到字符串中的\\\
,我如何计算lable的高度。请帮助我。
谢谢!!!
这个:
NSString * str = @这是\\\
\\\\\
CGSize size = [str sizeWithFont:[UIFont fontWithName:@Verdanasize:12.5] constrainedToSize:CGSizeMake(300.0f,999999.0f)];
NSLog(@Height:%f Width:%f,size.height,size.width);
这给我身高:48.000000宽度:40.000000
,但请务必将标签中的行号设置为 0
。
I am using below code to calculate the size for the label for a string.
NSString * str = @"It's \n that \n don't \n";
CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Verdana" size:12.5] forWidth:300
lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"Height : %f Width : %f ",size.height,size.width);
But id does not consider \n characters from the string to change the line. When I display this string on a label and set it numberOfLines property to some 4 or 5 , label does consider the \n and go to the next line. How can I calculate the height for the lable considering the \n's from the string. Please help me .
Thank You !!!
Try this:
NSString * str = @"It's \n that \n don't \n";
CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Verdana" size:12.5] constrainedToSize:CGSizeMake(300.0f, 999999.0f)];
NSLog(@"Height : %f Width : %f ",size.height,size.width);
This give me Height : 48.000000 Width : 40.000000
, but be sure to set the number of line in the label to 0
.
这篇关于iphone:sizeWithFont方法不会从字符串中识别'\'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!