This article describes how the height of the Uilabel for custom line spacing is obtained and requires an open source Uilabel control: Tttattributedlabel
Attached Https://github.com/TTTAttributedLabel/TTTAttributedLabel
After downloading, add to project inside, import header file
#import " TTTAttributedLabel.h "
Directly on the code
NSString *str =@"Uilabel Gets the height when custom line spacing is Uilabel, gets the height when custom line spacing is Uilabel, gets the height when customizing the line spacing, uilabel the height when customizing the line spacing, gets the height when the custom line spacing is Uilabel, Uilabel Gets the height when custom line spacing is Uilabel, and gets the height when custom line spacing is defined."; //Create TttlabelTttattributedlabel *tttlabel = [[Tttattributedlabel alloc] Initwithframe:cgrectmake ( -, -, -, -)]; Tttlabel.linebreakmode=nslinebreakbycharwrapping; Tttlabel.linespacing=6;//Set Line spacingTttlabel.font = [Uifont systemfontofsize: A]; 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 the Tttlabel//set the same properties as above Tttlabel first through nsmutableattributedstring, such as line spacing, fontNsmutableattributedstring *attrstring =[[Nsmutableattributedstring alloc] initwithstring:str]; //Customize the same line spacing as STR and TttattributedlabelNsmutableparagraphstyle *paragrapstyle =[[Nsmutableparagraphstyle alloc] init]; [Paragrapstyle setlinespacing:6]; //Set Line spacing[Attrstring addattribute:nsparagraphstyleattributename Value:paragrapstyle Range:nsmakerange (0, Str.length)]; //Set Font[Attrstring addattribute:nsfontattributename Value:[uifont systemfontofsize: A] Range:nsmakerange (0, Str.length)]; //gets the height of the uilabel of the custom line spacingCGFloat height = [Tttattributedlabel sizethatfitsattributedstring:attrstring withconstraints:cgsizemake ( -, maxfloat) Limitedtonumberoflines:0].height; //re-change the frame height of the TttlabelCGRect rect =Tttlabel.frame; Rect.size.height=height; Tttlabel.frame= rect;
IOS Uilabel get height when customizing line spacing