Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

enter image description here

However, other custom fonts are getting cut off on the left and bottom as this one is: enter image description here

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

func setNewFont(fontName:String,size:CGFloat)
    {
        self.font = UIFont(name: fontName, size: size)
        sizeToFit()
    }
like image 289
Aggressor Avatar asked Oct 21 '14 19:10

Aggressor


1 Answers

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/

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).

 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)
        }
    }
like image 112
Aggressor Avatar answered Oct 20 '22 09:10

Aggressor