UILabel is not auto-shrinking text to fit label size

后端 未结 17 606
感动是毒
感动是毒 2020-12-04 06:53

I have this strange issue, and im dealing with it for more than 8 hours now.. Depending on situation i have to calculate UILabels size dynamically,
e.g

相关标签:
17条回答
  • 2020-12-04 07:25

    In Swift 4 (Programmatically):

    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200.0, height: 200.0))
    label.adjustsFontSizeToFitWidth = true
    label.numberOfLines = 0
    
    label.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
    
    view.addSubview(label)
    
    0 讨论(0)
  • 2020-12-04 07:26

    In case you are still searching for a better solution, I think this is what you want:

    A Boolean value indicating whether the font size should be reduced in order to fit the title string into the label’s bounding rectangle (this property is effective only when the numberOfLines property is set to 1).

    When setting this property, minimumScaleFactor MUST be set too (a good default is 0.5).

    Swift

    var adjustsFontSizeToFitWidth: Bool { get set }
    

    Objective-C

    @property(nonatomic) BOOL adjustsFontSizeToFitWidth;
    

    A Boolean value indicating whether spacing between letters should be adjusted to fit the string within the label’s bounds rectangle.

    Swift

    var allowsDefaultTighteningForTruncation: Bool { get set }
    

    Objective-C

    @property(nonatomic) BOOL allowsDefaultTighteningForTruncation;
    

    Source.

    0 讨论(0)
  • 2020-12-04 07:27

    You can write like

    UILabel *reviews = [[UILabel alloc]initWithFrame:CGRectMake(14, 13,270,30)];//Set frame
    reviews.numberOfLines=0;
    reviews.textAlignment = UITextAlignmentLeft;
    reviews.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:12];
    reviews.textColor=[UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.8]; 
    reviews.backgroundColor=[UIColor clearColor];
    

    You can calculate number of lines like that

    CGSize maxlblSize = CGSizeMake(270,9999);
    CGSize totalSize = [reviews.text sizeWithFont:reviews.font 
                  constrainedToSize:maxlblSize lineBreakMode:reviews.lineBreakMode];
    
    CGRect newFrame =reviews.frame;
    newFrame.size.height = totalSize.height;
    reviews.frame = newFrame;
    
    CGFloat reviewlblheight = totalSize.height;
    
    int lines=reviewlblheight/12;//12 is the font size of label
    

    UILabel *lbl=[[UILabel alloc]init];
    lbl.frame=CGRectMake(140,220 , 100, 25);//set frame as your requirement
    lbl.font=[UIFont fontWithName:@"Arial" size:20];
    [lbl setAutoresizingMask:UIViewContentModeScaleAspectFill];
    [lbl setLineBreakMode:UILineBreakModeClip];
    lbl.adjustsFontSizeToFitWidth=YES;//This is main for shrinking font
    lbl.text=@"HelloHelloHello";
    

    Hope this will help you :-) waiting for your reply

    0 讨论(0)
  • 2020-12-04 07:29

    Swift 4, Xcode 9.4.1

    The solution that worked for me: I had a label within a collection view cell, and the label text was getting trimmed. Set the attributes as below on Storyboard

    Lines = 0
    LineBreak = Word Wrap
    Set yourlabel's leading and trailing constraint = 0 (using Autolayout)
    
    0 讨论(0)
  • 2020-12-04 07:30

    does not work if numberOfLines > 1 What i did made a condition like this-

    if(lblRecLocation.text.length > 100)
        lblRecLocation.font = [UIFont fontWithName:@"app_font_name" size:10];
    
    0 讨论(0)
  • 2020-12-04 07:31

    This is for Swift 3 running Xcode 8.2.1 ( 8C1002 )

    The best solution that I've found is to set a fixed width in your Storyboard or IB on the label. Set your constraints with constrain to margins. In your viewDidLoad add the following lines of code:

    override func viewDidLoad() {
                super.viewDidLoad()
    
                label.numberOfLines = 1
                label.adjustsFontSizeToFitWidth = true
                label.minimumScaleFactor = 0.5
            }
    

    This worked like a charm and it doesn't overflow to a new line and shrinks the text to fit the width of the label without any weird issues and works in Swift 3.

    0 讨论(0)
提交回复
热议问题