The size of the text that is used for dynamic calculation based on the text content, text sizes, and maximum dimensions.
-(Cgsize) Labelautocalculaterectwith: (nsstring*) Text FontSize: (cgfloat) FontSize MaxSize: (cgsize) MaxSize
{
nsmutableparagraphstyle* Paragraphstyle = [[Nsmutableparagraphstyle alloc]init];
paragraphstyle.linebreakmode=nslinebreakbywordwrapping;
nsdictionary* attributes [email protected]{nsfontattributename:[uifont systemfontofsize:fontsize], NSParagraphStyleAttributeName:paragraphStyle.copy};
If the system is iOS7.0;
Cgsize labelsize;
if (![ Text respondstoselector: @selector (boundingRectWithSize:options:attributes:context:)]) {
Labelsize = [text Sizewithfont:[uifont systemfontofsize:fontsize] constrainedtosize:maxsize lineBreakMode: Nslinebreakbywordwrapping];
}
If it's IOS6.0,
Else
{
Labelsize = [Text boundingrectwithsize:maxsize
options:nsstringdrawinguseslinefragmentorigin| Nsstringdrawingusesfontleading| Nsstringdrawingtruncateslastvisibleline
Attributes:attributes
Context:nil].size;
}
Labelsize.height=ceil (labelsize.height);
Labelsize.width=ceil (Labelsize.width);
return labelsize;
}
Functions that are called
Cgsize maxSize = Cgsizemake (Maxfloat, 40);
Cgsize titlesize = [self labelautocalculaterectwith:_publicdetail.name fontsize:15.0f maxsize:maxsize];
UILabel *label = [UILabel alloc]init];
Label.frame = CGRectMake (0, 0, titlesize.width, titlesize.height);
Ios-dynamically calculates the width of the text to define the frame size of the control.