开发者

UILabel numberOfLines

开发者 https://www.devze.com 2023-01-05 09:56 出处:网络
I need to adjust numberOfLines of label in cellForRowAtIndexPath. Label is in word wrap mode. However, the actual ca开发者_JAVA百科lculation of label\'s height is taking place in heightForRowAtIndexPa

I need to adjust numberOfLines of label in cellForRowAtIndexPath. Label is in word wrap mode. However, the actual ca开发者_JAVA百科lculation of label's height is taking place in heightForRowAtIndexPath.

How to calculate numberOfLines in cellForRowAtIndexPath? For now I'm just using some very big number.


From the Apple Docs:

To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.

Then you may use this technique in the heightForRowAtIndexPath method to determine how tall the cell should be.

Constants:

static const CGFloat kCellFontSize = 14.0;
static const CGFloat kCellWidth = 300.0;
static const CGFloat kCellHeightMax = 999.0;
static const CGFloat kCellPadding = 10.0;

Method:

CGSize maxSize = CGSizeMake( kCellWidth, kCellHeightMax );

CGSize labelSize = 
        [[self cellTextString] sizeWithFont: [self cellFont] 
                                constrainedToSize: maxSize 
                                    lineBreakMode: UILineBreakModeTailTruncation];

return (labelSize.height + (2 * kCellPadding));
0

精彩评论

暂无评论...
验证码 换一张
取 消