【问题标题】:sizeWithFont:constrainedToSize - iOS7sizeWithFont:constrainedToSize - iOS7
【发布时间】:2013-11-02 21:13:06
【问题描述】:

我在 iOS6 中使用了以下方法,但在 iOS7 中我遇到了错误

CGSize labelHeight = [tweetText sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(self.tweetsTableView.bounds.size.width - 84, 4000)];

下面的完整方法,关于如何修改iOS7的任何想法?

- (CGFloat)heightForCellAtIndex:(NSUInteger)index {

    NSDictionary *tweet = self.tweets[index];
    CGFloat cellHeight = 50;
    NSString *tweetText = tweet[@"text"];

    CGSize labelHeight = [tweetText sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(self.tweetsTableView.bounds.size.width - 84, 4000)];

    cellHeight += labelHeight.height;
    return cellHeight;
}

【问题讨论】:

    标签: ios7 sizewithfont


    【解决方案1】:

    我知道这是一个老问题和迟到的答案,但它仍然非常相关,

    此 sizeWithFont 方法现已弃用,此新方法效果最佳

    NSString *content = **Whatever your label's content is expected to be**
    CGSize maximumLabelSize = CGSizeMake(390, 1000);
    
    NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey: NSFontAttributeName];
    
    CGSize newExpectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;
    

    因此您可以将标签(或表格单元格等)调整为

    label.frame.size.height = newExpectedLabelSize.height;
    

    我希望这会有所帮助,干杯,吉姆。

    【讨论】:

      【解决方案2】:

      添加以下行:

      UIFont *font = [UIFont boldSystemFontOfSize:16];
      CGRect new = [string boundingRectWithSize:CGSizeMake(200, 300) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil];
      CGSize stringSize= new.size;
      

      【讨论】:

        猜你喜欢
        • 2011-06-29
        • 1970-01-01
        • 2014-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-06
        • 2013-11-07
        • 1970-01-01
        相关资源
        最近更新 更多