UILabel根据文字大小自动调整大小

时间:2014-03-26 07:43:14

标签: ios uilabel text-size

我有一个自定义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;

}

3 个答案:

答案 0 :(得分:4)

使用此方法:

/*! 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)];

答案 1 :(得分: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

答案 2 :(得分:0)

我曾经遇到过同样的问题。 我做的是...... 创建了一个表示UITableViewCell的类以及单元配置属性(height,width..etc)

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的数据源

希望它有所帮助!