Download address github source or demo download
Want to get all the fonts, as follows:
Gets all the font names
nsarray *familynames = [Uifont familynames];
NSLog (@ "All font names--%@", familynames);
One single line of text
For single-line text, use the Sizewithattributes method directly, as follows:
-(void) viewdidload {
[super viewdidload];
Gets all the font names
nsarray *familynames = [Uifont familynames];
NSLog (@ "All font names--%@", familynames);
Uilabel *lbl_text = [[Uilabel alloc]init];
Lbl_text.backgroundcolor = [Uicolor greencolor];
Lbl_text.text = @ "We are a family";
Sets the font for the label Helveticaneue Courier
uifont *fnt = [Uifont fontwithname:@ "Courier New" size:24.0f];
Lbl_text.font = fnt;
According to the font to get nsstring size
cgsize size = [Lbl_text.text sizewithattributes:[nsdictionary Dictionarywithobjectsandkeys : Fnt,nsfontattributename,nil]];
H
cgfloat Nameh = Size.Height of the name;
The name of the W
cgfloat namew = size.width;
Lbl_text.frame = CGRectMake (100,100, Namew,nameh);
[Self.view Addsubview:lbl_text];
}
As shown in figure:
Two or more lines of text
First, the numberoflines of Uilabel is set to 0, followed by-(CGRect) Boundingrectwithsize: (cgsize) Size options: (nsstringdrawingoptions) Options attributes: (Nsdictionary *) The attributes context: (Nsstringdrawingcontext *) to compute cgsize with the following code:
-(void) viewdidload {
[super viewdidload];
Gets all the font names
nsarray *familynames = [Uifont familynames];
NSLog (@ "All font names--%@", familynames);
[Self setmultiline];
}
-(void) setmultiline
{
Uilabel *lbl_text = [Uilabel new];
Lbl_text.font = [Uifont systemfontofsize:14];
Lbl_text.text = @ "text in linguistics, the visual form of written language, the ancient single word" Wen ", the combination of words called" word ", now together called" words ", the basic individual of the text is called" word ". In daily life, "writing" can also refer to written language, language, articles, words and so on. Visual symbol form, break the time and space limit of spoken language. such as Chinese characters, Latin alphabet. ";
Lbl_text.backgroundcolor = [Uicolor greencolor];
Lbl_text.numberoflines = 0;//Multiple lines display, compute height
lbl_text.textcolor = [Uicolor lightgraycolor];
Cgsize lblsize = [Lbl_text.text boundingrectwithsize:cgsizemake ([UIScreen mainscreen].bounds.size.width-20, MAXFLOAT ) Options:nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:[uifont SystemFontOfSize:14]} Context:nil].size;
Lbl_text.frame = CGRectMake (Ten, Lblsize.width, lblsize.height);
[Self.view Addsubview:lbl_text];
}
As shown in figure: