的UILabel sizeToFit仅适用于自动版式关闭 [英] UILabel sizeToFit only works with AutoLayout turned off

查看:197
本文介绍了的UILabel sizeToFit仅适用于自动版式关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用iOS 6的一个应用程序,并想知道为什么我的code,它曾经工作在iOS 5大不工作了。我有一个动态的UILabel单元,获取基于它所携带的文字调整。

这是自动布局打开:

这是自动布局关闭:

下面是我的code:

   - (CGFloat的)heightForText:(* NSString的)bodyText的
{#定义FONT_SIZE 13.0f
#定义CELL_CONTENT_WIDTH 280.0f
#定义CELL_CONTENT_MARGIN 5.0F    CGSize constraintSize = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2),20000.0f);
    CGSize labelSize = [bodyText的sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat的高度= MAX(labelSize.height,36.0f);
    //的NSLog(@高度=%F,高度);
    回报高+(CELL_CONTENT_MARGIN * 2);} - (CGFloat的)的tableView:(UITableView的*)电视heightForRowAtIndexPath:(NSIndexPath *)indexPath {    NSInteger的部分= [indexPath段];    开关(部分){
        情况下0:
            如果([self.text.text长度]!= 0)
            {
                返回[自我heightForText:self.text.text];
            }
            否则,如果([self.link.text长度]!= 0)
            {               返回60.0f;            }
}


解决方案

您已经证明的code是罚款,问题是你的手机在你的标签上的约束(这你不'内最有可能T显示)。

您标签需要有限制,使其从它的父的每一个边缘的距离固定。这可能已经是如此 - 如果是这样,现在的问题是,标签不知道如何来包装其行 - 你需要还可以设置 preferredMaxLayoutWidth 上的标签。

我发现的工具,如DCIntrospect调试自动布局问题时,都非常有帮助。例如,在这种情况下,它很难说,如果这是一个没有环绕文字大小合适的标签,如果标签是不够高正确显示的内容。

So I am making an app using iOS 6 and was wondering why my code that used to work great on iOS 5 does not work anymore. I have a cell with dynamic UILabel that gets adjusted based on the text that it carries.

This is with autolayout turned on:

This is with autolayout turned off:

Here is my code:

- (CGFloat)heightForText:(NSString *)bodyText
{

#define FONT_SIZE 13.0f
#define CELL_CONTENT_WIDTH 280.0f
#define CELL_CONTENT_MARGIN 5.0f

    CGSize constraintSize = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    CGSize labelSize = [bodyText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat height = MAX(labelSize.height, 36.0f);
    //   NSLog(@"height=%f", height);
    return height + (CELL_CONTENT_MARGIN * 2);

}



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

    NSInteger section = [indexPath section];

    switch (section) {


        case 0:
            if ([self.text.text length]!=0)
            {
                return [self heightForText:self.text.text];
            }
            else if ([self.link.text length]!=0)
            {

               return 60.0f;

            }
}

解决方案

The code you've shown is fine, the problem is most likely within the constraints you have on the label within your cell (which you don't show).

Your label needs to have constraints to make it a fixed distance from each edge of its superview. This may already be the case - if so, the problem is now that the label doesn't know how to wrap its lines - you need to also set the preferredMaxLayoutWidth on the label.

I have found that tools such as DCIntrospect are really helpful when debugging Autolayout issues. For example, in this situation, its hard to tell if this is a label of the right size that is not wrapping text, or if the label is not tall enough to show your content properly.

这篇关于的UILabel sizeToFit仅适用于自动版式关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆