IOS development-UILabel automatic line feed and highly adaptive

Source: Internet
Author: User

IOS development-UILabel automatic line feed and highly adaptive

This is mainly because someone on the Forum asked this question today. After solving the problem, Mark it by the way.


It is very simple, and the code is clearly written. Directly add the code.


UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (10,100,300, 50)]; label. text = @ "this afternoon, the city is cloudy to overcast with showers or thunderstorms. From this night to tomorrow there are cloudy with showers, and the rainfall can reach heavy rain. Northeast wind 5-6 level gust 7 level, gradually increased to 6-7 level gust 8 level. Today's highest temperature: around 26, the lowest temperature in the morning: around 22. Minimum temperature this morning: 21. Today's UV level: 2 levels, weak radiation intensity, appropriate protection. Tomorrow's Car Wash Index: 4 levels, the weather is rainy, not suitable for car wash. "; // Clear the background color label. backgroundColor = [UIColor clearColor]; // set the font color to a white label. textColor = [UIColor whiteColor]; // set the background color of the label to Black label. backgroundColor = [UIColor blackColor]; // The text Center shows the label. textAlignment = UITextAlignmentCenter; // you can specify a label for automatic line insertion. lineBreakMode = UILineBreakModeWordWrap; label. numberOfLines = 0; // adaptive height CGRect txtFrame = label. frame; label. frame = CGRectMake (10,100,300, txtFrame. size. height = [label. text boundingRectWithSize: CGSizeMake (txtFrame. size. width, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes: [NSDictionary dictionaryWithObjectsAndKeys: label. font, NSFontAttributeName, nil] context: nil]. size. height); label. frame = CGRectMake (10,100,300, txtFrame. size. height); [self. view addSubview: label];


:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.