First and foremost, the height of the text is calculated, and then the height of the label is set.
Calculate Text Height:
Hypothetical text:
  nsstring *strtest = Span class= "S3" >@ " Xinhua Beijing 12 month 16 Day power According to Xinhua news agency " Xinhua International " client report, " set off " 15 Day's hectic schedule starts with a breakfast. Just dawn, Kazakhstan's prime minister, Karim Masimov, came to the hotel where the prime minister was staying. -- Beijing building, inviting the Prime minister to have breakfast together. This is a simple breakfast, only a round table, two chairs, some simple Chinese and western meals. It was also a hearty breakfast, with two prime ministers on the table with a wide range of consensus on the Sino-Kazakhstan capacity cooperation. This is a unstoppable breakfast, this cooperation will not only benefit the people of China and Kazakhstan, but also bring long-term common interests to the countries in the region. Standing in the building 23 layer of revolving restaurant, Astana City panoramic view, here is suitable to ascend overlooking. "
In fact, there are two methods of iOS:
ios7.0 before use:
[strtestsizewithfont: contentfont Constrainedtosize:cgsizemake(screenweight -20 , linebreakmode ): Nslinebreakbywordwrapping];
Then use the following method is better:
cgrect frame = [ Strtest boundingrectwithsize : cgsizemake ( 220 1000 options : Nsstringdrawinguseslinefragmentorigin attributes : @{ nsfontattributename :[ Uifont systemfontofsize : 10 } context : nil ;
The attributes parameters are:
Nsfontattributename //Uifont, default Helvetica (Neue)
Nsparagraphstyleattributename //Nsparagraphstyle, default Defaultparagraphstyle
Nsforegroundcolorattributename //Uicolor, default Blackcolor
Nsbackgroundcolorattributename //Uicolor, default Nil:no background
Nsligatureattributename //NSNumber containing integer, default 1:default ligatures, 0:no ligatures
Nskernattributename //NSNumber containing floating point value, in points; amount to modify default kerning. 0 means Kerni NG is disabled.
Nsstrikethroughstyleattributename ; //NSNumber containing integer, default 0:no strikethrough
Nsunderlinestyleattributename //NSNumber containing integer, default 0:no underline
Nsstrokecolorattributename //Uicolor, default Nil:same as foreground color
Nsstrokewidthattributename //NSNumber containing floating point value, in percent of the font point size, default 0:no stroke; PO Sitive for stroke alone, negative for stroke and fill (a typical value for outlined text would is 3.0)
Nsshadowattributename ; //Nsshadow, default Nil:no shadow
Nstexteffectattributename //NSString, default nil:no text effect
Nsattachmentattributename //nstextattachment, default nil
Nslinkattributename //Nsurl (preferred) or nsstring
Nsbaselineoffsetattributename //NSNumber containing floating point value, in points; offset from baseline, default 0
Nsunderlinecolorattributename //Uicolor, default Nil:same as foreground color
Nsstrikethroughcolorattributename //Uicolor, default Nil:same as foreground color
Nsobliquenessattributename //NSNumber containing floating point value; skew to being applied to glyphs, default 0:no skew
Nsexpansionattributename //NSNumber containing floating point value; log of expansion factor to being applied to glyphs, D Efault 0:no Expansion
Note When creating a label:
The font of ①label must be the same as the font used to calculate it.
②label the NumberOfLines to be set to zero
The width of the ③label is set to the same width as the calculation
Content label in iOS highly adaptive