IOS: Set UILabel line spacing and iosuilabel line spacing
// Create UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (7, 5, TABLEVIEW_WIDTH-HEADIMGVIEW_HEIGHT-20-24-20, 0)]; // automatically wrap the label. numberOfLines = 0; // set the label content width CGFloat textWidth = 414; // obtain the data NSString * text = model. content; // create an NSMutableAttributedString instance and pass text to NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithString: text]; // create an NSMutableParagraphStyle instance NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init]; // set the line spacing [style setLineSpacing: 10.0f]; // determine whether the content length is greater, if not, set the content width to the row width (if the content is smaller than the row width, the Label length is too short. If the Label has a background color, the layout effect will be affected) NSInteger leng = textWidth; if (attStr. length <textWidth) {leng = attStr. length;} // set the attStr style [attStr addAttribute: NSParagraphStyleAttributeName value: style range: NSMakeRange (0, leng)] based on the given length and style; // The Label gets the attStr style label. attributedText = attStr; // Label adaptive size [label sizeToFit]; // set the Label height label. height = label. frame. size. height;
This method can not only set the Label line spacing, but also set the adaptive height.