How to adjust the UILabel width according to the String

一笑奈何 提交于 2019-12-11 09:27:01

问题


I have a UITableView, filled with custom cells. The custom cell has many labels. One label it takes different size strings from the db. What I want is for the next label to start right after the first label (at the end of the string of first lable). I used this code:

cell.Manufactlbl.adjustsFontSizeToFitWidth=YES;
CGSize lLabelSIze = [coffeeObj.price sizeWithFont: cell.Manufactlbl.fontforWidth:cell.Manufactlbl.frame.size.width lineBreakMode:cell.Manufactlbl.lineBreakMode];
cell.Manufactlbl.frame = CGRectMake(cell.Manufactlbl.frame.origin.x, cell.Manufactlbl.frame.origin.y, cell.Manufactlbl.frame.size.width, lLabelSIze.height);
cell.Manufactlbl.font=[UIFont systemFontOfSize:12];
cell.Manufactlbl.text=coffeeObj.price;
cell.Typelbl.frame=CGRectMake(cell.Manufactlbl.frame.origin.x + cell.Manufactlbl.frame.size.width, cell.Typelbl.frame.origin.y, cell.Typelbl.frame.size.width, cell.Typelbl.frame.size.height);
cell.Typelbl.font=[UIFont systemFontOfSize:12];
cell.Typelbl.text=coffeeObj.instrument;`

But this does not work.


回答1:


Try this.

CGSize size = [@"Your string" 
               sizeWithFont:[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:22] 
               constrainedToSize:CGSizeMake(500, CGFLOAT_MAX)];

 _Lable.frame = CGRectMake(05,05, 50, size.height);  

hope it's helpful for you..




回答2:


You can do something like this: size each label to fit (makes the label just big enough for the text), and then set the frame of each label to be dependent on the others. for example:

    [labelOne sizeToFit];
    [labelTwo sizeToFit];
    labelTwo.frame = CGRectMake(CGRectGetMaxX(labelOne.frame), labelTwo.origin.y, labelTwo.frame.size.width, labelTwo.frame.size.height);



回答3:


google NS(Attributed)String+Geometrics it solves it




回答4:


Try this code :

NSString *text1 = @"example text1 ";

CGSize maximumLabelSize = CGSizeMake(236,9999);

CGSize expectedLabelSize = [text1 sizeWithFont:[UIFont systemFontOfSize:12]
                             constrainedToSize:maximumLabelSize 
                                 lineBreakMode:UILineBreakModeCharacterWrap];

UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50 ,0, expectedLabelSize.width, expectedLabelSize.height)]; 
label1.font = [UIFont systemFontOfSize:12];
label1.text = text1;
NSString *text2 = @"example Text2";

expectedLabelSize = [text2 sizeWithFont:[UIFont systemFontOfSize:12]
                      constrainedToSize:maximumLabelSize 
                          lineBreakMode:UILineBreakModeCharacterWrap];

UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(label1.frame.origin.x + label1.frame.size.width ,0, expectedLabelSize.width, expectedLabelSize.height)]; 

label2.font = [UIFont systemFontOfSize:12];
label2.text = text2;

[self.view addSubview:label1];
[self.view addSubview:label2];


来源:https://stackoverflow.com/questions/11880690/how-to-adjust-the-uilabel-width-according-to-the-string

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