简体   繁体   English

CATextlayer改变FONT的大小以适合帧

[英]CATextlayer change size of FONT to fit the frame

I have a CATextlayer of a certain size and NSAttributedString text of unknown length. 我有一个CATextlayer具有一定规模和NSAttributedString未知长度的文本。

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. [编辑]正如nall指出的那样,我可以确定字符串长度,当然,这是用户输入的一些文本,我需要将其放入一个固定大小的框中。

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];

I ended up doing this: 我最终这样做了:

textlayer is a CATextlayer textlayer是一个CATextlayer

theString is a NSMutableAttributedString theString是一个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;
    }

Working for Swift 5 clean solution. Swift 5清洁解决方案工作。

1) Extend String 1)扩展字符串

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

2) Subclass the CATextLayer 2)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 3)现在转到您的代码而不是CATextLayer使用DynamicTextLayer

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

I believe you're asking the same thing as this question . 我相信你问的是这个问题

It looks like you have to iterate over a variety of font sizes to determine which will fit inside your rect. 看起来您必须迭代各种字体大小以确定哪些适合您的rect。

CATextLayer *textLayer;

[textLayer setWrapped: TRUE];

This will hopefully will work 希望这将有效

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

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