How to make NSStringDrawingContext shrink text?

前端 未结 3 881
Happy的楠姐
Happy的楠姐 2021-02-04 08:10

I\'m trying to use the attributed string API of iOS 6 to calculate the size of text and shrink the font size if necessary. However, I can\'t get it to work as the documentation

相关标签:
3条回答
  • 2021-02-04 08:19

    NSStringDrawingContext's minimumScaleFactor appears to be broken in iOS 7.

    As far as I can make out, even in iOS 6, it didn't work for drawing; it worked for measuring, so you could work out what would happen in a context where it does work for drawing, like a UILabel. That way, you know the correct minimum height for the label.

    Or, you could use the resulting scale factor to shrink the text yourself, in the knowledge that now it will fit.

    Example:

    - (void)drawRect:(CGRect)rect
    {
        // rect is 0,0,210,31
    
        NSMutableAttributedString* s = 
            [[NSMutableAttributedString alloc] initWithString: @"This is the army Mister Jones."];
        [s addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:20]} 
            range:NSMakeRange(0,s.length)];
    
        NSMutableParagraphStyle* para = [[NSMutableParagraphStyle alloc] init];
        para.lineBreakMode = NSLineBreakByTruncatingTail;
        [s addAttributes:@{NSParagraphStyleAttributeName:para} 
            range:NSMakeRange(0,s.length)];
    
        NSStringDrawingContext* con = [[NSStringDrawingContext alloc] init];
        con.minimumScaleFactor = 0.5;
    
        CGRect result = 
            [s boundingRectWithSize:rect.size 
                options:NSStringDrawingUsesLineFragmentOrigin context:con];
        CGFloat scale = con.actualScaleFactor;
        // ...(could have a check here to see if result fits in target rect)...
    
        // fix font to use scale factor, and draw
        [s addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:20*scale]} 
            range:NSMakeRange(0,s.length)];
        [s drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    
    }
    

    In iOS 6, scale is about 0.85 and you can use it as shown to shrink the text. But in iOS 7, scale remains at 1, suggesting that no shrinkage is happening and this feature of NSStringDrawingContext is now useless. I can't tell whether that's a bug or whether the feature has been deliberately abandoned.

    0 讨论(0)
  • 2021-02-04 08:21

    After googling for a long time I did not find a solution working under iOS7. Right now I use the following workaround, knowing that it is very ugly. I render a UILabel in memory, take a screenshot and draw that. UILabel is able to shrink the text correctly.

    But perhaps someone finds it useful.

        UILabel *myLabel = [[UILabel alloc] initWithFrame:myLabelFrame];
        myLabel.font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:16];
        myLabel.text = @"Some text that is too long";
        myLabel.minimumScaleFactor = 0.5;
        myLabel.adjustsFontSizeToFitWidth = YES;
        myLabel.backgroundColor = [UIColor clearColor];
    
        UIGraphicsBeginImageContextWithOptions(myLabelFrame.size, NO, 0.0f);
        [[myLabel layer] renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        [screenshot drawInRect:myLabel.frame];
    
    0 讨论(0)
  • 2021-02-04 08:26

    Just wanted to post this solution as I have been battling with this for a couple hours. I could never get the text area to scale down and would always have the last lines of text cut off.

    The solution for me was to not add a context and just set it to nil. I got this when looking at the example on this site. https://littlebitesofcocoa.com/144-drawing-multiline-strings

    Note after getting the size the box would draw at using

    pageStringSize = [myString boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrsDictionary context:nil];
    

    I still needed to loop through scaling down the font manually:

    while (pageStringSize.size.height > 140 && scale > 0.5) {
                scale = scale - 0.1;
                font = [UIFont fontWithName:@"Chalkboard SE" size:24.0 *scale];
                attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,[NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil];
    
                pageStringSize = [myString boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrsDictionary context:nil];
    
            }
    
    0 讨论(0)
提交回复
热议问题