Label in iOS sets the next label's frame according to the contents of the previous label

Source: Internet
Author: User

#import "ViewController.h"

@interface Viewcontroller ()

@property (Nonatomic,strong) UILabel *labelone;

@property (Nonatomic,strong) UILabel *labeltwo;

@property (nonatomic,copy) NSString *str;

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Additional setup after loading the view, typically from a nib.

NSString *[email protected] "SouFun is the world's largest real estate home network platform, page views and traffic in similar sites in the absolute leading position. With 9,000 employees, the network covers more than 300 cities and has subsidiaries and offices in more than 100 cities in China. September 2010 SouFun (stock code Sfun) successfully listed on the New York Stock Exchange. SouFun is the world's largest real estate home network platform, page views and traffic in similar sites in the absolute leading position. With 9,000 employees, the network covers more than 300 cities and has subsidiaries and offices in more than 100 cities in China. September 2010 SouFun (stock code Sfun) successfully listed on the New York Stock Exchange. ";

Self.str=strone;

Self.labelone=[[uilabel Alloc]init];

self.labelone.numberoflines=0;

Self.labelone.font=[uifont systemfontofsize:14];

Self.labelone.textalignment=nstextalignmentcenter;

Self.labelone.text=strone;

Self.labeltwo=[[uilabel Alloc]init];

self.labeltwo.numberoflines=0;

Self.labeltwo.font=[uifont SYSTEMFONTOFSIZE:20];

Self.labeltwo.textalignment=nstextalignmentcenter;

Self.labeltwo.text=strone;

[Self.view AddSubview:self.labelOne];

[Self.view AddSubview:self.labelTwo];

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

-(void) Viewwillappear: (BOOL) animated{

CGRect labeloneframe=[self.str boundingrectwithsize:cgsizemake (0) options: Nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:[uifont systemFontOfSize:14]} Context:nil] ; [Super viewwillappear:animated];

Cgpoint Pointone=cgpointmake (100, 50);

CGRect Labelframe;

Labelframe.origin=pointone;

Labelframe.size=labeloneframe.size;

Self.labelone.frame=labelframe;

CGRect labeltwoframe=[self.str boundingrectwithsize:cgsizemake (0) options: Nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:[uifont systemFontOfSize:20]} Context:nil] ;

Cgpoint Pointtwo=cgpointmake (self.labelone.frame.origin.x, Cgrectgetmaxy (self.labelOne.frame));

Labelframe.origin=pointtwo;

Labelframe.size=labeltwoframe.size;

Self.labeltwo.frame=labelframe;

}

@end

In fact, the automatic layout doesn't have to be so troublesome.

Label in iOS sets the next label's frame according to the contents of the previous 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.