sizeToFit削减一些UILabel字体类型高度&宽度但不是其他 - 有解决方法吗?

时间:2014-10-21 19:25:23

标签: ios swift fonts uilabel xcode6

我正在使用自定义字体,我有点麻烦。

有些字体可以正常使用sizeToFit,如下所示:

enter image description here

但是,其他自定义字体在左侧和底部被截断,因为这是: enter image description here

我可以' hack'它只是检查每种字体类型并添加一些像素,但我想知道是否有更清晰的解决方案,甚至是解释为什么会这样。

谢谢!

这是我的自定义UILabel字体设置代码

func setNewFont(fontName:String,size:CGFloat)
    {
        self.font = UIFont(name: fontName, size: size)
        sizeToFit()
    }

1 个答案:

答案 0 :(得分:6)

这篇文章解释了如何解决垂直问题:

http://www.andyyardley.com/2012/04/24/custom-ios-fonts-and-how-to-fix-the-vertical-position-problem/

然而,字体的左侧仍然被切断了。

所以除了上面的修复,我还覆盖了绘制调用并偏移了x值 (调整xml文件中的minLeftBearing没有修复任何东西,显然xcode忽略了它。)

 override func drawTextInRect(rect: CGRect)
    {
        if(font.fontName == "JennaSue")//JennaSue
        {
            var newBounds:CGRect = CGRectMake(rect.origin.x+2,rect.origin.y,rect.width,rect.height)
            super.drawTextInRect(newBounds)
        }else
        {
            super.drawTextInRect(rect)
        }
    }