iOS Uilabel automatically adjusts height based on content

Source: Internet
Author: User

One: Object method, Incoming: Font/maximum size. You can get a wide height,
The maximum size mainly limits the width, if it is a row to a {maxfloat,maxfloat}, if it is more than one line to limit the x value, y value to give

-(Cgsize) Sizewithfont: (Uifont *) font maxSize: (cgsize) maxsize{nsdictionary    *attrs = @{nsfontattributename:font} ;    return [self boundingrectwithsize:maxsize options:nsstringdrawinguseslinefragmentorigin attributes:attrs context: Nil].size;}

Two: Incoming text/font/maximum size. You can get the text width high

-(Cgsize) Sizewithtext: (NSString *) text font: (Uifont *) font MAXW: (cgfloat) maxw{nsmutabledictionary *attrs=[    Nsmutabledictionary dictionary];    Attrs[nsfontattributename]=font;        Cgsize Maxsize=cgsizemake (MAXW, maxfloat);   return [text boundingrectwithsize:maxsize options:nsstringdrawinguseslinefragmentorigin attributes:attrs context: Nil].size;} -(Cgsize) Sizewithtext: (NSString *) text font: (Uifont *) font{    return [self sizewithtext:text font:font MAXW: Maxfloat];}

Customize the location of the control settings uilable:

Self.namelabel.frame=cgrectmake (IconX, Icony, ICONWH, ICONWH);

Or:

    CGFloat TimeX = NameX;    CGFloat timey = Cgrectgetmaxy (self.namelabelf) + hwstatuscellborderw;    Cgsize timesize = [self sizeWithText:status.created_at font:hwstatuscelltimefont];    Self.timelabelf = (cgrect) {{TimeX, timey}, timesize};

iOS Uilabel automatically adjusts height based on content

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.