Question

I have a CATextlayer of a certain size and NSAttributedString text of unknown length.

I need to adjust the font-size so the text fits the frame (not vice versa :)

Any ideas where to start? :)

[Edit] as nall points out, I can determine the string length, of course, it's some text entered by the user that I need to fit into a box of fixed size.

Was it helpful?

Solution 3

I ended up doing this:

textlayer is a CATextlayer

theString is a NSMutableAttributedString

And yes, it's not very elegant and could definitely be improved ;)

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)theString);

    CGRect columnRect = CGRectMake(0, 0 , 320, 150);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, columnRect);

    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

    CFRange frameRange = CTFrameGetVisibleStringRange(frame); 

    int fontSize = 18;

    while(theString.string.length > frameRange.length){

        fontSize--;

        CFStringRef fontName = (__bridge CFStringRef)[defs objectForKey:@"font"];

        CTFontRef font = CTFontCreateWithName(fontName, fontSize, NULL);

        [theString addAttribute:(NSString *)kCTFontAttributeName
                          value:(__bridge id)font
                          range:NSMakeRange(0, theString.string.length)];

        CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)theString);

        CGRect columnRect = CGRectMake(0, 0 , 320, 150);

        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddRect(path, NULL, columnRect);

        CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

        frameRange = CTFrameGetVisibleStringRange(frame); 

        textLayer.string = theString;
    }

OTHER TIPS

I achieved it by doing this:

    float fontSize = InitialFontSize;
    UIFont *myFont = [UIFont boldSystemFontOfSize:fontSize];
    CGSize myFontSize = [YourTextHere sizeWithFont:myFont];
    while (myFontSize.width >= MaximunWidth) {
        fontSize -= 0.1f;
        myFont = [UIFont boldSystemFontOfSize:fontSize];
        myFontSize = [YourTextHere sizeWithFont:myFont];
    }
    CATextLayer *textLayer = [CATextLayer layer];
    [textLayer setFrame:CGRectMake(MaximunWidth - myFontSize.width / 2, MaximunHeight - myFontSize.height / 2, myFontSize.width, myFontSize.height)];
    [textLayer setFontSize:fontSize];
    [textLayer setString:YourTextHere];

    [textLayer setAlignmentMode:kCAAlignmentCenter];

Working for Swift 5 clean solution.

1) Extend String

extension String {
    func size(OfFont font: UIFont) -> CGSize {
        return (self as NSString).size(withAttributes: [NSAttributedString.Key.font: font])
    }
}

2) Subclass the CATextLayer

class DynamicTextLayer : CATextLayer {
    var adjustsFontSizeToFitWidth = false

    override func layoutSublayers() {
        super.layoutSublayers()
        if adjustsFontSizeToFitWidth {
            fitToFrame()
        }
    }

    func fitToFrame(){
        // Calculates the string size.
        var stringSize: CGSize  {
            get { return (string as? String)!.size(OfFont: UIFont(name: (font as! UIFont).fontName, size: fontSize)!) }
        }
        // Adds inset from the borders. Optional
        let inset: CGFloat = 2
        // Decreases the font size until criteria met
        while frame.width < stringSize.width + inset {
            fontSize -= 1
        }
    }
}

3) Now go to your code and instead of CATextLayer use DynamicTextLayer

textLayer = DynamicTextLayer()
textLayer?.alignmentMode = .center
textLayer?.fontSize = 40
textLayer?.string = "Example"
textLayer?.adjustsFontSizeToFitWidth = true
CATextLayer *textLayer;

[textLayer setWrapped: TRUE];

This will hopefully will work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top