iDia
iDia

Reputation: 1397

How to adjust the UILabel width according to the String

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.

Upvotes: 1

Views: 5856

Answers (4)

Muhammed Ayaz
Muhammed Ayaz

Reputation: 1398

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..

Upvotes: 4

Ankit
Ankit

Reputation: 1694

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];

Upvotes: 0

deleted_user
deleted_user

Reputation: 3805

google NS(Attributed)String+Geometrics it solves it

Upvotes: 0

wattson12
wattson12

Reputation: 11174

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);

Upvotes: 0

Related Questions