IOS-dynamically calculates the text width to define the frame size of the control ., Ios-frame
// Dynamically calculates the size occupied by the text based on the uploaded text content, text size, and maximum size.
-(CGSize) labelAutoCalculateRectWith :( NSString *) text FontSize :( CGFloat) fontSize MaxSize :( CGSize) maxSize
{
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
ParagraphStyle. lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary * attributes =@{ 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];
}
// For 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;
}
// Called Function
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 );