In the iOS development process, we often use UITableView, talk about UITableView of course uitableviewcell. So sometimes we have doubts, how to make the cell height according to the size of the text, And the height of the photo to dynamically design it?
Let's take a look at what we can do to make the cell's height dynamic change, so that the interface looks more visually coordinated?
- Dynamically set the cell's height
- + (CGFloat) Heightforrowwithmodel: (Photoinfo *) photoinfo
- {
- //1. Height of the picture
- //Let picture scale scale
- //(1) Get pictures
- UIImage *image = [UIImage imagewithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"ZZ" oftype:@"PNG"] ];
- CGFloat imageheight = [self heightforimage:image];
- //2. Height of text
- CGFloat textHeight = [self heightForText:photoInfo.introduction];
- //3. Return the total height of the cell
- return kphotocell_titlelabel_height + imageheight + textHeight + 4 * kphotocell_marginbetween;
- }
- Calculate the height of a picture individually
- + (CGFloat) Heightforimage: (UIImage *) image
- {
- //(2) Get the size of the picture
- Cgsize size = image.size;
- //(3) to find the scaling ratio
- CGFloat scale = Kphotocell_width/size.width;
- CGFloat imageheight = size.height * scale;
- return imageheight;
- }
- Calculate the height of the text individually
- + (CGFloat) Heightfortext: (NSString *) text
- {
- //Set the size of the font when calculating text and what criteria to calculate
- Nsdictionary *attrbute = @{nsfontattributename:[uifont systemfontofsize:kfontsize]};
- return [text Boundingrectwithsize:cgsizemake (kphotocell_width) options: Nsstringdrawinguseslinefragmentorigin | Nsstringdrawingusesfontleading Attributes:attrbute context:nil].size.height;
- }
In the code, K begins with the values defined by the macro.
iOS development dynamically calculates the height of the cell