2013-11-28 11 views
7

私はuitableviewセルのテキストラベルの高さを計算しています。 sizewithfontがios 7で廃止されたのを見た後、私はsizewithattributesを実装しましたが、返される値は、ラベルに含まれるテキストの正しいサイズであるべきラベルよりも小さくなりました。私もsizetofitメソッドを無駄にしようとしました。正しいサイズを返していない属性があります

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    NSDictionary *message = self.messages[indexPath.row]; 

    UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:1]; 
    UILabel *messageContent = (UILabel *)[cell.contentView viewWithTag:3]; 
    UIImageView *image = (UIImageView *)[cell.contentView viewWithTag:2]; 
    messageContent.text = [message objectForKey:@"messageContent"]; 
    NSString *content = [message objectForKey:@"messageContent"]; 
    NSLog(@"Message: %@", content); 

    CGSize textSize = [content sizeWithAttributes:@{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]}]; 
    messageContent.font = [UIFont fontWithName:@"HelveticaNue-Light" size:17.0]; 
    CGRect messageFrame = messageContent.frame; 
    messageFrame.size = textSize; 
    messageContent.frame = messageFrame; 


    nameLabel.text = [message objectForKey:@"senderName"]; 
    NSString *senderPicture = [message objectForKey:@"senderPicture"]; 
    UIImage* myImage = [UIImage imageWithData: 
        [NSData dataWithContentsOfURL: 
        [NSURL URLWithString: senderPicture]]]; 

    image.image = myImage; 
    image.layer.cornerRadius = 27.0; 
    image.layer.masksToBounds = YES; 

    //Configure the cell... 

    return cell; 
    } 

答えて

9

帰属:

user/Elio.d私は以下の彼の答えの写しを添付しました偉大な答えhere.

を持っています。それはあなたを助けている場合にElio.d upvoteを送信行くために必ずしてくださいhis original answer


トランスクリプト:

もあなたはこれを試すことができます。

NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0f]}; 
// NSString class method: boundingRectWithSize:options:attributes:context is 
// available only on ios7.0 sdk. 
CGRect rect = [textToMeasure boundingRectWithSize:CGSizeMake(width, MAXFLOAT) 
              options:NSStringDrawingUsesLineFragmentOrigin 
             attributes:attributes 
              context:nil]; 
+0

これは動作しますが、唯一の私はに自動レイアウト設定した場合IBの中でオフにして、私はラベルがuitableviewのセルの中のどこにもありません。私はそれらを持っています – user2489946

+5

この回答はあなたのものではなく、あなたのものです。 http://stackoverflow.com/questions/19145078/ios-7-sizewithattributes-replacement-for-sizewithfontconstrainedtosize – Brave

+1

@Braveのコピー貼り付けのような他の人の仕事はとても不自由です。私は彼のために適切な帰属を編集しました。うまくいけば、Elioにさらにアップホーを送るでしょう。 –

関連する問題