如何设置SKLabelNode的字体大小以适应固定大小(Swift)

时间:2015-06-22 13:14:10

标签: swift sprite-kit sklabelnode

我有一个正方形(200X200),其中有一个SKLabelNode。标签显示得分,我的数量很大。我想要适合广场上的数字。

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

3 个答案:

答案 0 :(得分:30)

可以将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)
}

<强>目标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);
}

答案 1 :(得分:1)

我已经为宽度写了这个,但你可以根据你的CGRect调整它的高度。在示例中,pg是使用您正在使用的字体初始化的SKLabelNode。参数是您的String和目标宽度,结果是您要分配给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
    }

答案 2 :(得分: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