简体   繁体   English

在UILabel(UITableViewCell的子视图)中调整文本大小以适合宽度

[英]Resize text in a UILabel (subview of UITableViewCell) to fit width

I have a UITableViewCell that is customized, sometime, text in UILabel ( UILabel is subview in UITableViewCell ) is exceeds size, and it get some dots (like ...) in label. 我有一个自定义的UITableViewCell ,有时, UILabel文本( UILabelUITableViewCell子视图)超出了大小,并且在标签中出现了一些点(例如...)。 I want to adjust font size to fit width. 我想调整字体大小以适合宽度。 But it does not work. 但这行不通。

Here is my code In Class implement for UITableviewCell 这是我对UITableviewCell的类实现中的代码

-(void) awakeFromNib{
    myLabel.minimumFontSize = 8;
    myLabel.adjustsFontSizeToFitWidth = YES;
}

Please help me!!! 请帮我!!!

Try this simple code 试试这个简单的代码

 UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    label.backgroundColor=[UIColor clearColor];
    label.text= yourString;
    label.numberOfLines=0;
    label.adjustsFontSizeToFitWidth=YES;
    label.minimumFontSize=6;
    label.textAlignment=UITextAlignmentCenter;
    [self.View addSubview:label];

    CGFloat fontSize = 20;
    while (fontSize > 0.0)
    {
        CGSize size = [yourString sizeWithFont:[UIFont fontWithName:@"Rockwell-Bold" size:fontSize] constrainedToSize:CGSizeMake(label.frame.size.width, 10000) lineBreakMode:UILineBreakModeWordWrap];

        if (size.height <= label.frame.size.height) break;

        fontSize -= 1.0;
    }

    //set font size
    label.font = [UIFont fontWithName:@"Rockwell-Bold" size:fontSize];
    [label release];

try this one... 试试这个...

    myLabel.numberOfLines=0;
    [myLabel sizeToFit];

at First. 首先。 In your TableViewClass , in heightForRowAtIndexPath you have to calculate the height of your text according to your label and return it. TableViewClassheightForRowAtIndexPath ,必须根据label计算textheight并将其return and make your label's autoLayout property flexible height . 并使标签的autoLayout属性flexible height You do not need to reset your label 's frame . 您无需重置labelframe try this it will work.... 试试这个,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM