Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel attributedText and minimumScaleFactor

Is it possible to use minimumScaleFactor and attributed text string at a same time?

        [myLabel setFrame:CGRectMake(6, 0, 200, 25)];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"A very long string and its first 20 characters should be bold"];
    [attStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:NSMakeRange(0, 20)];
    myLabel.attributedText = attStr;
    [myLabel setAdjustsFontSizeToFitWidth:YES];
    [myLabel setAdjustsLetterSpacingToFitWidth:YES];
    [myLabel setMinimumScaleFactor:0.3];

This doesn't seem to work. If I set myLabel.text it scales as expected. How I get scaling to work correctly?

like image 795
Timo Ylikännö Avatar asked Nov 17 '12 18:11

Timo Ylikännö


1 Answers

You can take the fist 20 character part and get its width, then you can take the rest part's width using same way:

CGSize maximumLabelSize = CGSizeMake(500.0,20.0);//write a really long width

//this will turn the expected length of the labels.. 
 CGSize expectedFirstLabelSize = [[[[NSMutableAttributedString alloc] initWithString:@"A very long string and its first 20 characters should be bold"] substringToIndex:21]
 sizeWithFont:[UIFont  boldSystemFontOfSize:17] constrainedToSize:maximumLabelSize lineBreakMode:nil];


 CGSize expectedLastLabelSize = [[[[NSMutableAttributedString alloc] initWithString:@"A very long string and its first 20 characters should be bold"] substringFromIndex:20] 
sizeWithFont:[UIFont  normalSystemFontOfSize:15] constrainedToSize:maximumLabelSize lineBreakMode:nil];

then you will know their length's rate.. for ex. first part is 140 px, rest is 260 px and your label area is 100 px, then u can create 2 different labels with 35 px and 65 px. And then you can use setAdjustsFontSizeToFitWidth for both of the labels.

like image 70
Mihriban Minaz Avatar answered Sep 18 '22 06:09

Mihriban Minaz