加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:36:15 所属栏目:百科 来源:网络整理
导读:我有一个方块(200X200),里面有一个SKLabelNode.标签显示得分,我将达到大量.我想要在广场上的数字. 如何更改SKLabelNode的文本大小(或大小)以使其适合固定大小. 可以将SKLabelNode的框架的大小与给定的矩形进行比较.如果按照标签的矩形和所需的矩形的大小成比
我有一个方块(200X200),里面有一个SKLabelNode.标签显示得分,我将达到大量.我想要在广场上的数字.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读