Automatic line feed of iOS UILabel, iosuilabel
Ideas:
Implementation:
UILabel * label = [[UILabel alloc] init];
Label. text = @ "Hello, this is the test content of UILabel's automatic line feed test. It mainly implements automatic line feed for multi-row data and adaptive data of different rows ";
NSDictionary * attribute =@{ NSFontAttributeName: [UIFont systemFontOfSize: 14]};
CGSize labelSize = [label. text boundingRectWithSize: CGSizeMake (200,500 0) options: Running | NSStringDrawingUsesLineFragmentOrigin | nsstringdrawingusesfontleadattributes: attribute context: nil]. size;
// 200 is the width of UILabel, and 5000 is a preset height, which indicates that
// Note: Previously, sizeWithFont: constrainedToSize: lineBreakMode: Method of the NSString class was used, but this method has been replaced by iOS7 Deprecated, while iOS7 has a new boudingRectWithSize: options: attributes: context: method.
Label. frame = CGRectMake (label. frame. origin. x, label. frame. origin. y, label. frame. size. width, labelSize. height );
// Keep the original Label position and width, but change the height.
Label. numberOfLines = 0; // indicates that the label can be displayed in multiple rows.
Label. textColor = UICOLOR_FROM_RGB (100,100,100, 1 );
Label. font = [UIFont systemFontOfSize: 14];
[Self. view addSubview: label];