nmo*_*ary 19 sprite-kit sklabelnode swift
我有一个正方形(200X200),里面有一个SKLabelNode.标签显示得分,我的数量很大.我想要适合广场上的数字.
如何更改a的文本大小(或大小)SKLabelNode以使其适合固定大小.
mik*_*663 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)
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6644 次 |
| 最近记录: |