UILabel height?

若如初见. 提交于 2019-12-06 14:57:40

问题


I have an uilabel setup with IB and with this code:

// setup label
sv.text =  @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."; 
sv.lineBreakMode = UILineBreakModeWordWrap; 
sv.numberOfLines = 0;
[sv sizeToFit];

my question is, how do I get the uilabel height?


回答1:


Try this one.....

            CGSize maximumSize1 = CGSizeMake(100, 9999);
            NSString *myString1 = @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.";
            UIFont *myFont1 = [UIFont boldSystemFontOfSize:14];//[UIFont fontWithName:@"Helvetica" size:14];


            UILabel *lbl_Title=[[UILabel alloc] init];
            lbl_Title.lineBreakMode=UILineBreakModeClip;
            CGSize myStringSize1 = [myString1 sizeWithFont:myFont1 
                                         constrainedToSize:maximumSize1 
                                             lineBreakMode:lbl_Title.lineBreakMode];
            lbl_Title.frame=CGRectMake(20, 10, myStringSize1.width, myStringSize1.height);
            lbl_Title.font=myFont1;
            //      lbl_Title.font=[UIFont boldSystemFontOfSize:14];
            lbl_Title.backgroundColor=[UIColor clearColor];
            lbl_Title.numberOfLines=100;
            lbl_Title.text=myString1;



回答2:


Check the bounds property.

sv.bounds.size.height



回答3:


either of the following should do the trick...

<ullabel>.frame.size.height
<uilabel>.bounds.size.height

Use NSLog to take a look...

NSLog(@"%f", myLabel.bounds.size.height);
NSLog(@"%f", myLabel.frame.size.height);


来源:https://stackoverflow.com/questions/3484218/uilabel-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!