【问题标题】:UILabel to size automatically according to text sizeUILabel 根据文本大小自动调整大小
【发布时间】:2014-05-04 11:17:15
【问题描述】:

我有一个自定义UITableViewCell,它有两个UILabels 和一个UIImageView。我根据来自 Web 服务的数据对象设置每个 UILabel's 文本。因此,直到运行时才知道文本的大小,因为它可能是来自我的 Web 服务调用的任何内容。

有没有办法告诉UILabel 根据其文本大小或内容自动调整其大小?

更新

好吧,由于某种原因,所有建议的方法都不会改变我在 UITableViewCell 中的 UILabel。

我在下面的代码中设置文字:

使用 SujithPt 方法的示例:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BBCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CategoryCell"];
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CategoryCell" owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];

    BBFilterCategoryObject *category = self.categories[indexPath.row];

    cell.title.text = category.category.name;
        CGSize labelSize = [self getSizeForText:cell.title.text maxWidth:150 font:@"ChaparralPro-Bold" fontSize:15];

    [cell.title sizeThatFits:labelSize];

    return cell;
}


- (CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {

    /*! Returns the size of the label to display the text provided
     @param text
     The string to be displayed
     @param width
     The width required for displaying the string
     @param fontName
     The font name for the label
     @param fontSize
     The font size for the label
     */

    CGSize constraintSize;
    constraintSize.height = MAXFLOAT;
    constraintSize.width = width;
    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,
                                          nil];

    CGRect frame = [text boundingRectWithSize:constraintSize
                                      options:NSStringDrawingUsesLineFragmentOrigin
                                   attributes:attributesDictionary
                                      context:nil];

                        CGSize stringSize = frame.size;

    return stringSize;

}

【问题讨论】:

标签: ios uilabel text-size


【解决方案1】:

我曾经遇到过同样的问题。 我所做的是... 创建了一个代表 UITableViewCell 以及单元格配置属性(高度、宽度..等)的类

class Cell{

 NSString * urlLabelString; //which will come from web service


 //it will return height of Cell which can be used in `-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath` delegate method
    -(float)height{

        //calculate size of label using [sizeWithFont][1]  method and add your image height here
        // and return total dynamic cell height
       return totalheight;
     }

}

您可以将这些类对象的数组保留为 UItableView 的数据源

希望对你有帮助!

【讨论】:

    【解决方案2】:
    //use this for custom font
    CGFloat width =  [label.text sizeWithAttributes:@{NSFontAttributeName:
    [UIFont fontWithName:@"ChaparralPro-Bold" size:40 ]}].width;
    
    //use this for system font 
    CGFloat width =  [label.text sizeWithAttributes:@{NSFontAttributeName
    :[UIFont systemFontOfSize:40 ]}].width;
    
    label.frame = CGRectMake(point.x, point.y, width,height);
    
    //point.x, point.y -> origin for label;
    //height -> your label height; 
    

    here得到它

    【讨论】:

    • sizeWithFont 在 iOS 7.0 中已弃用。
    • +1 @RajPatil 对此做出了较小的回答。请注意,答案的“将其用于系统字体”部分有一个重复的分号。
    【解决方案3】:

    使用这个方法:

    /*! Returns the size of the label to display the text provided
        @param text
            The string to be displayed
        @param width
            The width required for displaying the string
        @param fontName
            The font name for the label
        @param fontSize
            The font size for the label
     */
    - (CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {
        CGSize constraintSize;
        constraintSize.height = MAXFLOAT;
        constraintSize.width = width;
        NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              [UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,
                                              nil];
    
        CGRect frame = [text boundingRectWithSize:constraintSize
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:attributesDictionary
                                          context:nil];
    
        CGSize stringSize = frame.size;
        return stringSize;
    }
    

    编辑

    使用上述方法返回的大小设置标签框。

    [cell.title setFrame:CGRectMake(cell.title.frame.origin.x, cell.title.frame.origin.y, sizeFromTheMethod.height, sizeFromTheMethod.width)];
    

    【讨论】:

    • 这似乎有效 - 因为在逐步执行该方法时 stringSize 设置正确。但是我的标签没有正确改变大小。我错过了什么?
    • 您会在设置标签属性的位置发布代码吗?
    • 更新了我的问题。谢谢。
    • 你只是在调用我发布的方法。使用它的返回大小来设置您的标签高度。
    • 我更新了问题。我应该那样做吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多