我有一个 UILabel,其字体大小要在捏合手势 iOS swift 上增加。我可以增加字体大小,但 View 有问题。认为 sizeToFit() 根据需要增加高度和宽度,但它没有反射(reflect)在 View 上,即标签在 View 中保持不变。请帮我随着字体大小的增加而增加标签的大小。
@IBAction func increaseTextFont(sender: UIPinchGestureRecognizer) {
var pinchScale = sender.scale
pinchScale = round(pinchScale * 1000) / 1000.0
if (pinchScale < 1) {
testLabel.font = UIFont( name: "arial", size: testLabel.font.pointSize - pinchScale)
}
else{
testLabel.font = UIFont( name: "arial", size: testLabel.font.pointSize + pinchScale)
}
testLabel.frame.height * pinchScale))
testLabel.frame.size.height *= pinchScale
testLabel.frame.size.width *= pinchScale
self.testLabel.layoutIfNeeded()
print(testLabel.frame.size.height)
print(testLabel.frame.size.width)
}
最佳答案
出于演示目的,我做了一个测试演示,您可以看到相同的结果,
标签的约束如下,
您可能需要根据您必须设计的 UI 或要求更改约束,只需确保您没有设置标签的高度约束并且如果您必须使标签多行,则相应地设置约束和标签属性。
捏合手势的代码,
@IBOutlet 弱变量 labelTest:UILabel!
@IBAction func increaseTextFont(sender: UIPinchGestureRecognizer) {
var pinchScale = sender.scale
pinchScale = round(pinchScale * 1000) / 1000.0
if (pinchScale < 1) {
labelTest.font = UIFont( name: "arial", size: labelTest.font.pointSize - pinchScale)
}
else{
labelTest.font = UIFont( name: "arial", size: labelTest.font.pointSize + pinchScale)
}
}
您还需要根据要求限制文本的增长。
希望这对您有帮助。
关于ios - 增加捏合手势中的字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36026621/