user3572586
user3572586

Reputation:

How To Count how many characters can come in a UILabel line?

I want to make a UITableview with dynamic height for cell. In each cell i am displaying a message of varying text. Now i want that my table cell be according to the text of the message and also my label present on cell should display the text. I am making my table cell flexible on the basis of the number of characters that can accommodate in a line. But here is the problem. Different alphabets take different space. So, i am unable to calculate how many alphabets can come in a single line. Any Suggestions to this???

Upvotes: 6

Views: 3453

Answers (5)

soprof
soprof

Reputation: 326

You don't want to count how many characters your text has, just count the height using:

h = [NSString stringWithFormat:@"%f",
                           [post.text boundingRectWithSize:CGSizeMake(298.0f, CGFLOAT_MAX)
                           options:(NSStringDrawingUsesLineFragmentOrigin)
                           attributes:[NSDictionary dictionaryWithObject:[UIFont fontWithName:@"Helvetica Neue" size:14.0]
                           forKey: NSFontAttributeName]
                           context:nil].size.height]

Use your width for 298 and your font/fontsize.

Upvotes: 1

Kumar KL
Kumar KL

Reputation: 15335

Try this :

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{CGSize size;
        if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
            // code here for iOS 5.0,6.0 and so on
            CGSize fontSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17]];
            size = fontSize;
        }
        else {
            // code here for iOS 7.0
            NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                                  [UIFont fontWithName:@"Helvetica Neue" size:19], NSFontAttributeName,
                                                  nil];
            CGRect fontSizeFor7 = [text boundingRectWithSize:CGSizeMake(571, 500)
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                                  attributes:attributesDictionary
                                                     context:nil];
            size = fontSizeFor7.size;

        }return size.height +30 ;

    }

Upvotes: 2

Pancho
Pancho

Reputation: 4143

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

  NSString * yourCellStr = @"The string you want to apply to your cell at this index path";
  CGSize maxLblSize = CGSizeMake(320, 200);
  CGSize expectedLblSize = [yourCellStr sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:maxLblSize lineBreakeMode:NSLineBreakByWordWrapping];

  return expectedLblSize.height;

}

Upvotes: 0

user3560423
user3560423

Reputation: 17

Please my below answer

UILabel *lbl = [[UILabel alloc] init];
 lbl.text = @"abcd";
 NSLog(@"count %lu",(unsigned long)[lbl.text length]);

Upvotes: -1

Suyog Patil
Suyog Patil

Reputation: 1616

Check my answer

Calculate UITableViewCell height to fit string

Here you have to calculate the height of label according to the text and then set to height of the cell.

You can calculate the height and width dynamically using this.

I hope it will help you

Upvotes: 0

Related Questions