【发布时间】:2015-02-26 21:23:24
【问题描述】:
有没有办法确定adjustsFontSizeToFitWidth 是否被触发?我的应用程序有一个占据整个视图的 UILabel。通过使用 UIPinchGestureRecognizer,您可以通过捏合和张开来更改字体大小。效果很好。但是,当字体达到 UILabel 的最大大小时,它会显示奇怪的行为。 UILabel 中的文本在 UILabel 中向下移动。如果不添加 bannerLabel.adjustsFontSizeToFitWidth = true,文本将被剪切。我想知道 UILabel 的字体何时达到最大尺寸,我将停止尝试增加字体大小。
import UIKit
class ViewController: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet weak var bannerLabel: UILabel!
var perviousScale:CGFloat = 0
var fontSize:CGFloat = 0
var originalFontSize: CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
let pinchGesture = UIPinchGestureRecognizer(target: self, action: Selector("pinch:"))
view.addGestureRecognizer(pinchGesture)
perviousScale = pinchGesture.scale
bannerLabel.adjustsFontSizeToFitWidth = true
originalFontSize = bannerLabel.font.pointSize
fontSize = originalFontSize
}
func pinch(sender:UIPinchGestureRecognizer) {
println("font size \(bannerLabel.font.pointSize)")
if perviousScale >= sender.scale //Zoom In
{
decreaseFontSize()
}
else if perviousScale < sender.scale //Zoom Out
{
increaseFontSize()
}
}
func threeFingers(sender:UIPanGestureRecognizer) {
println("threeFingers")
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
bannerLabel.font = UIFont(name: bannerLabel.font.fontName, size: fontSize)
}
func increaseFontSize(){
bannerLabel.font = UIFont(name: bannerLabel.font.fontName, size: fontSize)
fontSize = fontSize + 2.5
}
func decreaseFontSize(){
bannerLabel.font = UIFont(name: bannerLabel.font.fontName, size: fontSize)
if fontSize >= originalFontSize {
fontSize = fontSize - 2.5
}
}
}
【问题讨论】: