简体   繁体   English

sizeToFit削减一些UILabel字体类型的高度和宽度但不是其他 - 是否有修复?

[英]sizeToFit Cuts off Some UILabel Font Types Height & Width But Not Others — Is There A Fix?

I am working with custom fonts and I have a bit of a snag. 我正在使用自定义字体,我有一点障碍。

Some of the fonts work just fine with sizeToFit like this: 有些字体可以正常使用sizeToFit,如下所示:

在此输入图像描述

However, other custom fonts are getting cut off on the left and bottom as this one is: 但是,其他自定义字体在左侧和底部被截断,因为这是: 在此输入图像描述

I could 'hack' it and just check for every font type and add a few pixels but I'd like to know if there is a cleaner solution or even an explanation as to why this is happening. 我可以“破解”它,只检查每种字体类型并添加几个像素,但我想知道是否有更清晰的解决方案,甚至解释为什么会发生这种情况。

Thanks! 谢谢!

Here is my custom UILabel font setting code 这是我自定义的UILabel字体设置代码

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

This post explained how to fix the vertical problem: 这篇文章解释了如何解决垂直问题:

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

However the left side of the font was still getting cut off. 然而,字体的左侧仍然被切断。

So in addition to the fix above, I also overrode the draw call and offset the x value (adjust the minLeftBearing in the xml file did NOT fix anything, clearly xcode ignores it). 所以除了上面的修复,我还覆盖了绘制调用并偏移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)
        }
    }

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

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