How to make UILabel / UITableViewCell to have a dynamic height

來源:互聯網
上載者:User

I recently found out, that even though according to the documentation sizeToFit
on a UILabel
will take into account the numberOfLines
property, it simply doesn't.

You have seen variable heights of UITableViewCell
in iPhone applications like App Store, Address Book, SMS and so on. These applications have a UITableView
as main part, but each cell in the table has a variable height according to text amount it holds.

With UITableView
it is quite different if you're coming from a HTML world,
as UITableView
cannot generate a dynamic height automatically
, you'll need to calculate the height yourself before you set the delegate.

How to make UILabel / UITableViewCell to have a dynamic height

After you created a UITableView
, you will need to set a delegate
and a datasource
. There is a UITableViewDelegate
method to tell UITableView
how tall a cell would be:

  1. - (
    CGFloat
    )tableView:(
    UITableView
     *)tableView  
  2.            heightForRowAtIndexPath:(NSIndexPath
     *)indexPath;  

- (CGFloat)tableView:(UITableView *)tableView<br /> heightForRowAtIndexPath:(NSIndexPath *)indexPath;<br />

If you want UITableViewCell
to have a dynmic height,
you'll need to calculate the height of text block and return the result
in the method I mentioned above.

There are four NSString
methods that can do the calculations:

  1. - (
    CGSize
    )sizeWithFont:(
    UIFont
     *)font constrainedToSize:(
    CGSize
    )size;  
  2. - (CGSize
    )sizeWithFont:(
    UIFont
     *)font constrainedToSize:(
    CGSize
    )size  
  3.           lineBreakMode:(UILineBreakMode
    )lineBreakMode;  
  4. - (CGSize
    )sizeWithFont:(
    UIFont
     *)font forWidth:(
    CGFloat
    )width  
  5.           lineBreakMode:(UILineBreakMode
    )lineBreakMode;  
  6. - (CGSize
    )sizeWithFont:(
    UIFont
     *)font  
  7.           minFontSize:(CGFloat
    )minFontSize  
  8.           actualFontSize:(CGFloat
     *)actualFontSize  
  9.           forWidth:(CGFloat
    )width  
  10.           lineBreakMode:(UILineBreakMode
    )lineBreakMode;  

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size;<br />- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size<br /> lineBreakMode:(UILineBreakMode)lineBreakMode;<br />- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width<br /> lineBreakMode:(UILineBreakMode)lineBreakMode;<br />- (CGSize)sizeWithFont:(UIFont *)font<br /> minFontSize:(CGFloat)minFontSize<br /> actualFontSize:(CGFloat *)actualFontSize<br /> forWidth:(CGFloat)width<br /> lineBreakMode:(UILineBreakMode)lineBreakMode;<br />

To calculate the exact height of a text block, you'll need to
specify a large CGSize and those methods will return you exact size,
here's an example:

  1. CGSize
     aSize;  
  2.   
  3. aSize = [aString sizeWithFont:[UIFont
     systemFontOfSize:14]  
  4.                  constrainedToSize:CGSizeMake
    (300.0, 1000.0)  
  5.                  lineBreakMode:UILineBreakModeTailTruncation
    ];  

CGSize aSize;<br />aSize = [aString sizeWithFont:[UIFont systemFontOfSize:14]<br /> constrainedToSize:CGSizeMake(300.0, 1000.0)<br /> lineBreakMode:UILineBreakModeTailTruncation];<br />

Then you can get the height by accessing aSize.height
.

What I'm doing most of the time is to call one of those methods in
the model class when setting the string and storing the height in the
model class, so you can use the correct height right away.

Now to size the UILabel
and/or UITableViewCell
you just need to set the frame
property of UILabel
and/or UITableViewCell
.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.