调整UILabel的大小,没有质量损失

时间:2015-01-02 11:01:18

标签: ios cocoa-touch swift

我希望能够使用UIPinchGesture调整UILabel的大小,而不会造成质量损失。目前我可以调整它,虽然它变得非常模糊。

这是我的代码

label = UILabel(frame:CGRectMake(0,100,300,50))
    label.text = "Hellow World"
    label.textColor = UIColor.blueColor()
    label.font = UIFont(name: "HelveticaNeue-Bold", size: self.label.frame.height)
    self.view.addSubview(label)

    self.resizeRecognizer = UIPinchGestureRecognizer(target: self, action: Selector("handleScale:"))
    self.rotateRecognizer = UIRotationGestureRecognizer(target: self, action: Selector("handleRotate:"))

    label.userInteractionEnabled = true
    label.addGestureRecognizer(self.resizeRecognizer)

这是我的手势识别器

func handleScale(recognizer: UIPinchGestureRecognizer)
{
    recognizer.view!.transform = CGAffineTransformScale(recognizer.view!.transform,
        recognizer.scale, recognizer.scale)
    recognizer.scale = 1
        recognizer.view!.frame = CGRectMake(recognizer.view!.frame.minX, recognizer.view!.frame.minY, recognizer.view!.frame.width, recognizer.view!.frame.height)

    println(recognizer.view!.frame)
    println(self.label.font.fontWithSize(recognizer.view!.frame.height))
    println(self.label.frame)

}

这里需要做的是缩放标签框架时,文本的大小也需要改变。 因此,当我更改框架时,字体大小应更改为label.font = UIFont(name: "HelveticaNeue-Bold", size: self.label.frame.height)

这就是我的目标

<UICTFont: 0x7ffb39e17ba0> font-family: "Helvetica Neue"; font-weight: bold; font-style: normal; font-size: 578.77pt
<UICTFont: 0x7ffb3ca03ed0> font-family: "Helvetica Neue"; font-weight: bold; font-style: normal; font-size: 594.29pt

字体大小会随着帧的调整而调整,我尝试从头开始制作字体大小并使其呈现清晰。 我的想法是,实际标签没有再次重新渲染。 有人知道发生了什么或如何解决它?

编辑:

顶部的文字是我想在重新缩放后发生的事情,底部的文字是正在发生的事情

Top = Correct, Botton = Incorrect

我实现这一点的方式是我创建了一个新标签,其中包含旧标签的大小,然后将其添加到子视图中,我希望能够在重新缩放时执行此操作,如果这有任何意义

4 个答案:

答案 0 :(得分:1)

我认为您没有更新字体大小,fontWithSize会返回您应该分配给标签的字体。

  func handleScale(recognizer: UIPinchGestureRecognizer)
{
    self.test.transform = CGAffineTransformScale(self.test.transform, recognizer.scale, recognizer.scale)
    recognizer.scale = 1


    println(recognizer.view!.frame)


    self.test.font = self.test.font.fontWithSize(recognizer.view!.frame.height)
    println(self.test.font)
    println(self.test.frame)


}

答案 1 :(得分:0)

创建一个UILabel,其字体大小适合您的最大缩放比例。然后使用transform属性将其缩放到其初始大小。另外,将adjustsFontSizeToFitWidth设置为false。

答案 2 :(得分:0)

我已经复制了你的代码并确实弄明白,上面的代码只是更新标签框架。实际上字体大小没有按预期更新。

按照以下代码,您可以清楚地看到差异。

import UIKit

class ViewController: UIViewController {

    var resizeRecognizer: UIPinchGestureRecognizer!
    var rotateRecognizer: UIRotationGestureRecognizer!
    var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        label = UILabel(frame:CGRectMake(50,300,300,50))
        label.text = "Hello World"
        label.textColor = UIColor.blueColor()
        label.font = UIFont(name: "HelveticaNeue-Bold", size: 18)
        label.backgroundColor = UIColor.darkGrayColor()
        self.view.addSubview(label)

        self.resizeRecognizer = UIPinchGestureRecognizer()
        self.resizeRecognizer.addTarget(self, action: Selector("handleScale:"))
        label.userInteractionEnabled = true
        label.addGestureRecognizer(self.resizeRecognizer)
    }

    func handleScale(recognizer: UIPinchGestureRecognizer)
    {
        recognizer.view!.transform = CGAffineTransformScale(recognizer.view!.transform,
            recognizer.scale, recognizer.scale)
        recognizer.scale = 1
        recognizer.view!.frame = CGRectMake(0, recognizer.view!.frame.minY, recognizer.view!.frame.width, recognizer.view!.frame.height)

        label.frame = CGRectMake(recognizer.view!.frame.minX, recognizer.view!.frame.minY, recognizer.view!.frame.width, recognizer.view!.frame.height)
        label.font = UIFont(name: self.label.font.fontName, size: recognizer.view!.frame.height)

        // Or Suggested solution
        var fontSize = CGFloat(recognizer.view!.frame.height / 2)

        label.font = UIFont(name: self.label.font.fontName, size: recognizer.view!.frame.height - fontSize)

    }
}

答案 3 :(得分:0)

试试这个

import UIKit

class ViewController: UIViewController {

let label = UILabel()
let pinchRecognizer = UIPinchGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()

    label.text = "first line\nsecond line"
    label.textAlignment = NSTextAlignment.Center
    label.font = UIFont(name: "Menlo-Regular", size: 25)
    label.numberOfLines = 0
    label.sizeToFit()
    label.center = self.view.center
    self.view.addSubview(label)

    pinchRecognizer.addTarget(self, action: "changeFontSize")
    self.view.addGestureRecognizer(pinchRecognizer)
}

func changeFontSize() {

    if pinchRecognizer.state == .Began {

        pinchRecognizer.scale = label.font.pointSize * 0.1
    }

    if 1 <= pinchRecognizer.scale && pinchRecognizer.scale <= 5  {

        let cent = label.center
        label.font = UIFont(name: label.font.fontName, size: pinchRecognizer.scale * 10)
        label.frame.size.width = 999
        label.numberOfLines = 2
        label.sizeToFit()
        label.center = cent 
    }
}


}