【问题标题】:Any way to determine if adjustsFontSizeToFitWidth has triggered?有什么方法可以确定 adjustsFontSizeToFitWidth 是否已触发?
【发布时间】: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
        }
    }
}

【问题讨论】:

    标签: swift ios8 xcode6 uilabel


    【解决方案1】:

    是的,你可以添加一个观察者:

    1) 将动态修饰符添加到您要观察的属性中:

    dynamic UILabel bannerLabel = UILabel()
    

    2) 创建全局上下文变量

    private var myContext = 0
    

    3) 为 key-path 添加一个观察者

    bannerLabel.addObserver(self, forKeyPath: "adjustsFontSizeToFitWidth", options:.New, context:&myContext)    
    

    4) 覆盖 observeValueForKeyPath 并移除 deinit 中的观察者

    override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject: AnyObject], context: UnsafeMutablePointer<Void>) {
            if context == &myContext {
                println("Font size adjusted to fit width: \(change[NSKeyValueChangeNewKey])")
            } else {
                super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
            }
        }
    
     deinit {
            bannerLabel .removeObserver(self, forKeyPath: "adjustsFontSizeToFitWidth", context: &myContext)
        }
    

    【讨论】:

    • 谢谢。我如何实现这一点,我以前从未使用过.addObserver。添加该行时,出现以下错误:Cannot invoke 'addObserver' with an argument list of type '(ViewController, forKeyPath: String, options: String, context: nil)'
    • dynamic UILabel bannerLabel = UILabel() 给了我以下错误Consecutive declarations on a line must be separated by ';'
    猜你喜欢
    • 2010-10-28
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2015-08-19
    • 2012-05-30
    • 1970-01-01
    相关资源
    最近更新 更多