计算文字在某个字体以及某个linebreakmode下的长度来适应label和textfield

 

 

/*
 4 down vote accepted
 
 
 The NSString additions to UIKit for drawing text, you can pre-determine the exact amount of space required to render a given text for a given font. If splitting the text into pages, you could use this method.
 
 – sizeWithFont:constrainedToSize:lineBreakMode:
 
 Assuming the font and line break mode is known, create a CGSize having the same width as your page, and use a sufficiently number for height. This would be the maximum size that we are constraining the text into.
 
 CGSize maximumSize = CGSizeMake(pageWidth, 999999999);
 CGSize expectedSize = [veryLongString sizeWithFont:theFont constrainedToSize:maximumSize lineBreakMode:theLineBreakMode];
 
 expectedSize will tell us the size that the text will take assuming if it could extend vertically infinitely (well close to). To find the number of pages required, just divide the total height by the height of one page.
 
 NSInteger totalPages = ceil(expectedSize.height / heightOfOnePage);
 
 You would also want to adjust the height of one page to make sure that the last line of text doesn't get clipped. For that to happen, the height of the page should be a multiple of the font's line height. Say the initial page height is 300px, and the font-height is 16px, then there will be some clipping as 300/16 = 18.75 which is not a whole number.
 
 NSInteger linesWithoutClipping = floor(initialPageHeight / theFont.lineHeight);
 CGFloat optimalPageHeight = linesWithoutClipping * theFont.lineHeight;
 
 Taking the floor value 18 and multiplying with the font line height 16, we get an optimal page height of 288 to ensure there's no clipping.
 
 Note that lineHeight was introduced in iOS 4.0, but you could calculate it yourselves if needed for older versions.

 */
@implementation MNViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIFont* theFont = [UIFont systemFontOfSize:18];
    //Calculate the size necessary for the UILable
    UITextField* theLabel = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
    theLabel.borderStyle = UITextBorderStyleRoundedRect;
    theLabel.font = theFont;
    //theLabel. = NSLineBreakByWordWrapping;
    [self.view addSubview:theLabel];
    NSString *veryLongString = @"Textingfdafdsdafdfdsdfadasfdasfda";
    CGFloat pageHeight = 30;
    CGSize maximumSize = CGSizeMake(999999, pageHeight);
    NSLineBreakMode theLineBreakMode = NSLineBreakByWordWrapping;//此字段似乎没有影响。如果此时是label要设置NSLineBreakByWordWrapping=lineBreakMode,才能正好显示下所有的文字宽度
    CGSize expectedSize = [veryLongString sizeWithFont:theFont constrainedToSize:maximumSize lineBreakMode:theLineBreakMode];
    // 14是我测试出来的 当borderStyle是UITextBorderStyleRoundedRect时,文本显示框对称剧中,大概位置是左右各7
    // 按照需求 我应该使用borderStyle=UITextBorderStyleNone,此时文本框和字的宽度刚好
    theLabel.frame = (CGRect){0,0,expectedSize.width+14,30};
    NSLog(@"expectedSize:%@",NSStringFromCGSize(expectedSize));
   
    theLabel.text = veryLongString;
//    CGSize theStringSize = [theText sizeWithFont:font
//                               constrainedToSize:theLabel.frame.size
//                                   lineBreakMode:theLabel.lineBreakMode];
//    NSLog(@"theStringSize:%@",NSStringFromCGSize(theStringSize));
//    //Adjust the size of the UILable
//    theLabel.frame = CGRectMake(theLabel.frame.origin.x,
//                                theLabel.frame.origin.y,
//                                theStringSize.width, theStringSize.height);
//    theLabel.text = theText;
}

输出结果:

uitextfield:

UITextBorderStyleRoundedRect

UITextBorderStyleNone

 

你可能感兴趣的:(method,计算,required,drawing,amount)