UILabel *testlabel = [[UILabel alloc] Initwithframe:cgrectmake (ten, 0, 0)]; Testlabel.backgroundcolor = [Uicolor whitecolor]; Testlabel.text = @ "We all have a home, the name is China, the house is climbing two dragons"; Testlabel.font = [Uifont systemfontofsize:20]; Testlabel.textcolor = [Uicolor blackcolor]; [TestLabel Sizethatfits:cgsizemake (20, 20)];//calculates the optimal size but does not change its size, and the individual thinks that this is the label's adaptive size and nothing else is useful NSLog (@ " TestLabel sizethatfits frame =%@ ", Nsstringfromcgrect (Testlabel.frame)); NSLog (@ "Best size =%@", nsstringfromcgsize ([TestLabel sizethatfits:cgsizemake (+)])); [TestLabel sizetofit];//calculates the optimal size and changes its size NSLog (@ "TestLabel sizetofit frame =%@", Nsstringfromcgrect ( Testlabel.frame));
[Self.view Addsubview:testlabel];
iOS UIView SizeToFit sizethatfits