在UILabel上使用捏手势调整字体和标签框架的大小? [英] Resize font along with frame of label using pinch gesture on UILabel?

查看:188
本文介绍了在UILabel上使用捏手势调整字体和标签框架的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户使用捏手势调整标签大小时,即可平滑地增大或减小字体大小.

Increase or decrease font size smoothly whenever user resize label using pinch gesture on it.

注意

  • 在不影响字体质量的情况下
  • 不仅改变了UILabel的规模
  • 支持多行文字
  • 旋转手势应与捏手势配合使用
  • 参考:SnapChat或Instagram文本编辑器工具
extension String {
    func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: UIFont(name: font.fontName, size: font.pointSize)!], context: nil)
        return ceil(boundingBox.height)
    }

    func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: UIFont(name: font.fontName, size: font.pointSize)!], context: nil)
        return ceil(boundingBox.width)
    }
}

func resizeLabelToText(textLabel : UILabel)
{
    let labelFont = textLabel.font
    let labelString = textLabel.text
    let labelWidth : CGFloat = labelString!.width(withConstrainedHeight: textLabel.frame.size.height, font: labelFont!)
    let labelHeight : CGFloat = labelString!.height(withConstrainedWidth: labelWidth, font: labelFont!)

    textLabel.frame = CGRect(x: textLabel.frame.origin.x, y: textLabel.frame.origin.y, width: labelWidth, height: labelHeight)
    textLabel.font = labelFont
}

func pinchedRecognize(_ pinchGesture: UIPinchGestureRecognizer) {
    guard pinchGesture.view != nil else {return}

    if (pinchGesture.view is UILabel) {
        let selectedTextLabel = pinchGesture.view as! UILabel

        if pinchGesture.state == .began || pinchGesture.state == .changed {
            let pinchScale = round(pinchGesture.scale * 1000) / 1000.0
            if (pinchScale < 1) {
                selectedTextLabel.font = selectedTextLabel.font.withSize(selectedTextLabel.font.pointSize - pinchScale)
            }
            else {
                selectedTextLabel.font = selectedTextLabel.font.withSize(selectedTextLabel.font.pointSize + pinchScale)
            }
            resizeLabelToText(textLabel: selectedTextLabel)
        }
    }
}

推荐答案

我用以下代码解决了该问题,该代码可以很好地处理所提及的各个方面,类似于Snapchat和Instagram:

var pointSize: CGFloat = 0
@objc func pinchRecoginze(_ pinchGesture: UIPinchGestureRecognizer) {
    guard pinchGesture.view != nil else {return}

    let view = pinchGesture.view!
    if (pinchGesture.view is UILabel) {
        let textLabel = view as! UILabel

        if pinchGesture.state == .began {
            let font = textLabel.font
            pointSize = font!.pointSize

            pinchGesture.scale = textLabel.font!.pointSize * 0.1
        }
        if 1 <= pinchGesture.scale && pinchGesture.scale <= 10  {
            textLabel.font = UIFont(name: textLabel.font!.fontName, size: pinchGesture.scale * 10)

            resizeLabelToText(textLabel: textLabel)
        }
    }
}

func resizeLabelToText(textLabel : UILabel) {
    let labelSize = textLabel.intrinsicContentSize
    textLabel.bounds.size = labelSize
}

这篇关于在UILabel上使用捏手势调整字体和标签框架的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆