Sometimes when we write a project, we encounter, feedback, and other places, such as the details of the lecture activities need a large display of text,
Because each time the server returns a different size, you need to dynamically adjust the label's width height:
In iOS 6, you can:
-(void) creatlabel{//dynamically determine label width based on word count_namelabel=[[UILabel alloc]init]; _namelabel.frame= CGRectMake ( -, -, -, +); _namelabel.backgroundcolor=[Uicolor Redcolor]; _namelabel.text=@"bodhi tree without trees"; //sets the font of the label Helveticaneue CourierUifont *font = [Uifont fontwithname:@"Helveticaneue"Size24.0f]; _namelabel.font=font; //get nsstring size according to the fontCgsize size =[_namelabel.text sizewithattributes:[nsdictionary dictionarywithobjectsandkeys:font,nsfontattributename, nil] ]; //the height of the nameCGFloat Nameh =Size.Height; //the width of the nameCGFloat Namew =Size.width; _namelabel.frame= CGRectMake ( -, -, Namew, Nameh); [Self.view Addsubview:_namelabel];}
When iOS 7 is available:
-(void) creattextfield{//dynamically determine the content of a frame based on the contents of the body_content=[[UILabel alloc]init]; _content.frame= CGRectMake ( -, $, self.view.frame.size.width- the, -); _content.backgroundcolor=[Uicolor Redcolor]; _content.clipstobounds=YES; _content.layer.cornerradius=Ten; _content.numberoflines=0; _content.layer.bordercolor=[Uicolor Graycolor]. Cgcolor; _content.layer.borderwidth=0.8; _content.text=@"Today the weather is fine and sunny today, the weather is fine today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine, today the weather is fine today, the weather is fine today, today the weather is fine, today the weather is fine and sunny today, Today the weather is fine and sunny today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine, today the weather is fine and sunny today, Today the weather is fine and sunny today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine and sunny today, the weather is fine today, today the weather is fine, today the weather is fine and sunny today, The weather is fine today."; //widthCGFloat CONTENTW = self.view.bounds.size.width- the; //the font of the label Helveticaneue CourierUifont *fnt = [Uifont fontwithname:@"Helveticaneue"Size18.0f]; _content.font=fnt; //IOS7 replaces SizeWithFont:constrainedToSize:lineBreakMode in obsolete iOS6 using the following methods:CGRect Tmprect= [_content.text boundingrectwithsize:cgsizemake (CONTENTW, +) Options:nsstringdrawinguseslinefragmentorigin attributes:[nsdictionary dictionarywithobjectsandkeys:fnt, Nsfontattributename, nil] context:nil]; //height HCGFloat Contenth =TmpRect.size.height; NSLog (@"adjusted display width:%f, display height:%f", Contentw,contenth); _content.frame= CGRectMake ( -, $, Contentw,contenth); [Self.view addsubview:_content];}
This can be a good solution to this problem!
iOS dynamically determines label width based on word count