21

我有一个正方形(200X200),里面有一个SKLabelNode。标签显示分数,它可能会达到一个很大的数字。我想把数字放在正方形里。

如何更改 a 的文本大小(或大小)SKLabelNode以使其适合固定大小。

4

3 回答 3

32

SKLabelNode 框架的大小可以与给定的矩形进行比较。如果根据标签矩形和所需矩形的大小按比例缩放字体,则可以确定最佳字体大小以尽可能多地填充空间。最后一行方便地将标签移动到矩形的中心。(如果文本只是小写字母或标点符号等短字符,它可能看起来偏离中心。)

迅速

func adjustLabelFontSizeToFitRect(labelNode:SKLabelNode, rect:CGRect) {

    // Determine the font scaling factor that should let the label text fit in the given rectangle.
    let scalingFactor = min(rect.width / labelNode.frame.width, rect.height / labelNode.frame.height)

    // Change the fontSize.
    labelNode.fontSize *= scalingFactor

    // Optionally move the SKLabelNode to the center of the rectangle.
    labelNode.position = CGPoint(x: rect.midX, y: rect.midY - labelNode.frame.height / 2.0)
}

Objective-C

-(void)adjustLabelFontSizeToFitRect:(SKLabelNode*)labelNode rect:(CGRect)rect {

    // Determine the font scaling factor that should let the label text fit in the given rectangle.
    double scalingFactor = MIN(rect.size.width / labelNode.frame.size.width, rect.size.height / labelNode.frame.size.height);

    // Change the fontSize.
    labelNode.fontSize *= scalingFactor;

    // Optionally move the SKLabelNode to the center of the rectangle.
    labelNode.position = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect) - labelNode.frame.size.height / 2.0);
}
于 2015-07-21T16:58:48.510 回答
2

我已经为宽度写了这个,但是您可以将其调整为适合您的 CGRect 的高度。在示例中, pg 是一个 SKLabelNode,使用您正在使用的字体进行初始化。参数是您的字符串和目标宽度,结果是您要分配给 SKLabelNode 的大小。当然你也可以直接放你的SKLabelNode。如果尺寸太大,则最大尺寸为 50,但这是个人的。

 func getTextSizeFromWidth(s:String, w:CGFloat)->CGFloat {

    var result:CGFloat = 0;
    var fits:Bool = false
    pg!.text=s
    if(s != ""){
      while (!fits) {
        result++;
        pg!.fontSize=result
        fits = pg!.frame.size.width > w;
      }
    result -= 1.0
    }else{
        result=0;
    }

    return min(result, CGFloat(50))
}

编辑:实际上,我刚刚意识到我也写了这个:

extension SKLabelNode {
func fitToWidth(maxWidth:CGFloat){
    while frame.size.width >= maxWidth {
        fontSize-=1.0
    }
}

func fitToHeight(maxHeight:CGFloat){
    while frame.size.height >= maxHeight {
        fontSize-=1.0
    }
于 2015-08-30T17:46:43.153 回答
0

mike663 答案的这种扩展对我有用,并且比一次走 1 个像素要快得多。

// Find the right size by trial & error...
var testingSize: CGFloat = 0    // start from here
var currentStep: CGFloat = 16   // go up by this much. It will be reduced each time we overshoot.
var foundMatch = false

while !foundMatch {
    var overShot = false
    while !overShot {
        testingSize += currentStep
        labelNode.fontSize = testingSize
        // How much bigger the text should be to perfectly fit in the given rectangle.
        let scalingFactor = min(rect.width / labelNode.frame.width, rect.height / labelNode.frame.height)

        if scalingFactor < 1         { overShot   = true }  // Never go over the space
        else if scalingFactor < 1.01 { foundMatch = true }  // Stop when over 99% of the space is filled
    }
    testingSize -= currentStep  // go back to the last one that didn't overshoot
    currentStep /= 4
}
labelNode.fontSize = testingSize // go back to the one we were happy with
于 2019-07-09T15:53:32.423 回答