IOS UILabel obtains the height when customizing the row spacing. iosuilabel
This article describes how to obtain the UILabel height for custom row spacing. You need to use the open-source UILabel control: TTTAttributedLabel.
Attached https://github.com/TTTAttributedLabel/TTTAttributedLabel
After the download, add it to the project and import the header file
#import "TTTAttributedLabel.h"
Directly Add code
NSString * str = @ "UILabel obtains the height when customizing the row spacing, and UILabel obtains the height when customizing the row spacing, the height is obtained when UILabel customizes the row spacing, when UILabel customizes the row spacing, and when UILabel customizes the row spacing. "; // create tttLabel TTTAttributedLabel * tttLabel = [[TTTAttributedLabel alloc] initWithFrame: CGRectMake (30, 55,300,100)]; tttLabel. lineBreakMode = NSLineBreakByCharWrapping; tttLabel. lineSpacing = 6; // set the row spacing tttLabel. font = [UIFont systemFontOfSize: 12]; tttLabel. numberOfLines = 0; // set the number of rows to 0 [tttLabel setText: str]; tttLabel. textAlignment = NSTextAlignmentLeft; tttLabel. backgroundColor = [UIColor redColor]; [self. view addSubview: tttLabel]; // get the height of tttLabel // first set the same attributes as tttLabel through NSMutableAttributedString, such as row spacing and font NSMutableAttributedString * attrString = [NSMutableAttributedString alloc] initWithString: str]; // customize the line spacing between str and TTTAttributedLabel * paragrapStyle = [[NSMutableParagraphStyle alloc] init]; [paragrapStyle setLineSpacing: 6]; // set the line spacing [attrString addAttribute: NSParagraphStyleAttributeName value: paragrapStyle range: NSMakeRange (0, str. length)]; // set the font [attrString addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 12] range: NSMakeRange (0, str. length)]; // obtain the height of UILabel for custom row spacing CGFloat height = [TTTAttributedLabel failed: attrString withConstraints: CGSizeMake (300, MAXFLOAT) limitedToNumberOfLines: 0]. height; // re-change the frame height of tttLabel CGRect rect = tttLabel. frame; rect. size. height = height; tttLabel. frame = rect;