Frame Style Layout Paragraph sub, set width height

Source: Internet
Author: User

Calculate the width and height of a text
/** *  计算一段文字的宽高 * *  @param size       这段文字的最大宽高 *  @param options    NSStringDrawingUsesLineFragmentOrigin *  @param attributes 文字的字体属性 *  @param context    nil * *  @return 返回的是 CGRect 类型,frame */- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);
The first method calculates the height of the text 过时的方法
    //  首先设置文字的宽和高    CGFloat textW = ?;    CGSize textMaxWH = CGSizeMake(textW, ?);    //设置文字的行数 设为0->会自动换行    text_label.numberOfLines = 0; CGFloat textH = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:textMaxWH].height;
The second method calculates the height of the text
    //  首先设置文字的宽和高    CGFloat textW = ?;    CGSize textMaxWH = CGSizeMake(textW, ?);    //设置文字的行数 设为0->会自动换行    text_label.numberOfLines = 0; //设置内部文字的字体属性 NSDictionary *attr = @{ NSFontAttributeName : [UIFont systemFontOfSize:14] }; CGFloat textH = [text boundingRectWithSize:textMaxWH options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size.height;
Calculates the width of a sentence of text
    //设置内部文字的字体属性    NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};    CGFloat textW = [text sizeWithAttributes:attr].width;

Frame Style Layout Paragraph sub, set width height

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.