繁体   English   中英

使用自动收缩调整UILabel文本

[英]Adjusting UILabel text with Autoshrink

背景

我正在创建一个应用程序,并且在其中有一个动画,当呈现键盘时,该动画可以重新定位UILabelUITextField 设置如下:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(raiseTextField), name: UIKeyboardWillShowNotification, object: nil)

这是函数:

func raiseTextField(sender: NSNotification){
    UIView.animateWithDuration(0.25, animations: { () -> Void in
        self.userInputTextFieldBottomContstraint.constant = sender.userInfo![UIKeyboardFrameEndUserInfoKey]!.CGRectValue().size.height
        self.view.layoutIfNeeded()
    })
}

我的故事板如下所示: 故事板

我已经设置了约束,以便friendInputView的大小正确,并且具有Autoshrink,因此字体会相应地进行调整。

问题

当我调用raiseTextField() ,一切正常,除了friendInputView的字体大小需要很长时间才能更改。 不是更新动画正在发生的,它后来更新。

最终,它的确会调整,只是没有以理想的速度调整。

我尝试在animateWithDuration()内部添加self.friendInputLabel.adjustsFontSizeToFitWidth = true ,但似乎没有任何改变。

例子

抬起键盘之前:

(文字调整正确)

之前

抬起键盘后立即:

(文本大小太大)

中间

抬起键盘大约一秒钟:

(文字调整正确)

后

底线

有什么方法可以使UILabel以编程方式调整其文本大小,而不是仅等待其自身更新? layoutIfNeeded()如何重新调整约束类似,是否有类似的函数可以重新调整自动收缩?

编辑1

我最终根据一个回答说使用UITextFieldDelegate而不是NSNotificationCenter使其工作。 我用以下代码替换了raiseTextField()函数:

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {

    self.userInputTextFieldBottomContstraint.constant = 250
    self.view.setNeedsUpdateConstraints()
    UIView.animateWithDuration(0.25, animations: { () -> Void in

        self.view.layoutIfNeeded()
    })

    return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()

    self.userInputTextFieldBottomContstraint.constant = 10
    self.view.setNeedsUpdateConstraints()

    UIView.animateWithDuration(0.25, animations: { () -> Void in
        self.view.layoutIfNeeded()
    })

    return true
}

但是,现在我有一个新问题: 如何使用实际的键盘尺寸? 假设键盘大小始终为250,不是很有帮助。

我尝试通过创建此变量来这样做:

var keyboardHeight: CGFloat?

把它放在viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(setTheKeyboardHeight), name: UIKeyboardWillShowNotification, object: nil)

并创建此功能:

func setTheKeyboardHeight(sender: NSNotification){
    keyboardHeight = sender.userInfo![UIKeyboardFrameEndUserInfoKey]?.CGRectValue().height
}

然后我将其添加到textFieldShouldBeginEditing()如下所示:

self.userInputTextFieldBottomContstraint.constant = keyboardHeight!

self.view.setNeedsUpdateConstraints()

UIView.animateWithDuration(0.25, animations: { () -> Void in
    self.view.layoutIfNeeded()
})

return true

但是,它最终只会给出错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

我认为是因为在调用textFieldShouldBeginEditing() 之后设置了keyboardHeight ,但是我不知道如何解决它。

使用textFieldShouldBeginEditing() ,有什么方法可以访问键盘高度,还是必须使用NSNotificationCenter

我已经完成了与您相同的演示,并观察到UIKeyboardNotification的某些怪异行为,但我不确定这是怪异的还是仅此而已,

我观察到的是,当您更改UIKeyboardNotification中的任何约束时,即使您没有使用UIView animationWithDuration块,它也会随着动画更改约束常数(或帧)。 所以我认为这种延迟可能是由于KeyboardNotification添加的动画效果所致,

func raiseTextField(sender: NSNotification){

    self.bottomConstraintTextField.constant = 250//sender.userInfo![UIKeyboardFrameEndUserInfoKey]!.CGRectValue().size.height
    self.view.setNeedsUpdateConstraints()

    //As you can see i have commented the animation block, still it changes the constraint with animation.
    //UIView.animateWithDuration(0.25, animations: { () -> Void in

        self.view.layoutIfNeeded()
    //})
}

因此,在您的方案中,我已在textFields委托中添加了动画代码,它可以按预期工作,

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {

    self.bottomConstraintTextField.constant = 250//sender.userInfo![UIKeyboardFrameEndUserInfoKey]!.CGRectValue().size.height
    self.view.setNeedsUpdateConstraints()
    UIView.animateWithDuration(0.25, animations: { () -> Void in

    self.view.layoutIfNeeded()
    })

    return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()

    self.bottomConstraintTextField.constant = 10
    self.view.setNeedsUpdateConstraints()

    UIView.animateWithDuration(0.25, animations: { () -> Void in
        self.view.layoutIfNeeded()
    })

    return true
}

结果是这样

在此处输入图片说明

希望对您有帮助。

更新:

我已经对其进行了处理,并找到了一种解决方法,因此您可以按照以下方法进行操作,而不是使用变量来保持键盘高度,

func raiseTextField(sender: NSNotification){

    self.bottomConstraintTextField.constant = sender.userInfo![UIKeyboardFrameEndUserInfoKey]!.CGRectValue().size.height
    self.view.setNeedsUpdateConstraints()

    self.view.layoutIfNeeded()
}


func textFieldShouldBeginEditing(textField: UITextField) -> Bool {

    self.bottomConstraintTextField.constant = 200
    self.view.setNeedsUpdateConstraints()

    UIView.animateWithDuration(0.25, animations: { () -> Void in

    self.view.layoutIfNeeded()
    })

    return true
}

因此,请同时具有textField委托和KeyboardNotification并更改textField委托中的底部约束,它将在键盘通知之前被调用,并且通过这样做,您将不会观察到缩小标签文本的延迟。

请参阅下面的更新效果,

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM