0

I am using a font called: Luckiest Guy in a Swift project (Link to the font). However, in UILabels, it's not vertically aligned to the center even after setting baselineAlignment to the center... Could you check my code and help me out. Thanks a lot!!

I attached a screen shot here to show the problem (https://www.dropbox.com/s/1hxq68dewtzzkxi/IMG_9733.png?dl=0)

    var dynamicLabel: UILabel = UILabel()

    dynamicLabel.frame = CGRectMake(0, 0, 60, 35) //Frame (the coordinates are changed here to simply the code)

    // *** Color and offset *** //
    dynamicLabel.backgroundColor = UIColor.blueColor()
    dynamicLabel.textColor = UIColor.whiteColor()
    dynamicLabel.shadowColor = UIColor.blackColor()
    dynamicLabel.shadowOffset = CGSizeMake(0, 1.0)

    // *** Text alignment *** //
    dynamicLabel.textAlignment = NSTextAlignment.Center
    dynamicLabel.font = UIFont(name: "LuckiestGuy-Regular", size: 25)
    dynamicLabel.adjustsFontSizeToFitWidth = true
    dynamicLabel.minimumScaleFactor = 0.5
    dynamicLabel.baselineAdjustment = UIBaselineAdjustment.AlignCenters

    // *** Label corner and border *** //
    dynamicLabel.layer.masksToBounds = true
    dynamicLabel.layer.cornerRadius = 15.0
    dynamicLabel.layer.borderColor = UIColor.whiteColor().CGColor
    dynamicLabel.layer.borderWidth = 1.0


    // Text
    dynamicLabel.text = "Excellent!"
Yanyan
  • 92
  • 9

1 Answers1

0

As your explanation to what is happening instead is rather vague I assume that the Label isn't really visible, because with the implementation of dynamicLabel.textAlignment = NSTextAlignment.Center the text should be aligned

I've tried your code in a sample project and the only two things I noticed that were off were the x and y coordinates of the label (as they're set to zero, so the label doesn't really get displayed anyways) and that you didn't add the label to the view:

    let dynamicLabel = UILabel(frame: CGRectMake(100, 200, 200, 100))

    // *** Color and offset *** //
    dynamicLabel.backgroundColor = UIColor.blueColor()
    dynamicLabel.textColor = UIColor.whiteColor()
    dynamicLabel.shadowColor = UIColor.blackColor()
    dynamicLabel.shadowOffset = CGSizeMake(0, 1.0)

    dynamicLabel.text = "Excellent!"

    // *** Text alignment *** //
    dynamicLabel.textAlignment = NSTextAlignment.Center
    dynamicLabel.adjustsFontSizeToFitWidth = true
    dynamicLabel.minimumScaleFactor = 0.5
    dynamicLabel.baselineAdjustment = UIBaselineAdjustment.AlignCenters

    // *** Label corner and border *** //
    dynamicLabel.layer.masksToBounds = true
    dynamicLabel.layer.cornerRadius = 15.0
    dynamicLabel.layer.borderColor = UIColor.whiteColor().CGColor
    dynamicLabel.layer.borderWidth = 1.0

    view.addSubview(dynamicLabel)
Julia Grill
  • 93
  • 2
  • 9
  • Hi Julia, thanks for your comments. Sorry for the part that is not clear. In my code, I actually added the label to the view. My problem is that the center of the text is not exactly in the center of my label.. As I'm a newbie, I am not allowed to upload pictures here. But you can check the final effect I got here: https://www.dropbox.com/s/1hxq68dewtzzkxi/IMG_9733.png?dl=0 Thanks for taking your time – Yanyan Aug 26 '15 at 11:46
  • No worries! What I've found was this blog post that explains how to fix this centring problem for custom fonts: [Post](http://www.andyyardley.com/2012/04/24/custom-ios-fonts-and-how-to-fix-the-vertical-position-problem/) // also: there's a Stackoverflow thread that already discussed the same problem of yours: [Thread](http://stackoverflow.com/a/8314197/4748900) – Julia Grill Aug 26 '15 at 12:53
  • Thanks a lot, Julia! The solution described in the two links you sent fixed my problem perfectly! Cheers! :) – Yanyan Aug 26 '15 at 19:53