0

I am using custom font in my application. I have set this to label, button and textfield. But when I used the custom font then text alignment of all object is changed. Its display from top side.

Here is the label with default font :

enter image description here

Here is the label with Custom font :

enter image description here

Here is the code :

    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.backgroundColor = [UIColor redColor];
    [lbl setFont:[UIFont fontWithName:@"HelveticaNeueLTStd-ThCn" size:[lbl.font pointSize]]];

Any pointers?

Amar
  • 13,202
  • 7
  • 53
  • 71
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56
  • Each font has its kerning & pattern. You should fine tune the text appearance by re-positioning the label – Raptor Sep 25 '13 at 10:05
  • @ShivanRaptor is there any way in which we can set proper alignment from coding ? This is because it will create a problem when we set different different font from coding. – Nirmalsinh Rathod Sep 25 '13 at 10:10
  • possible duplicate of [Custom installed font not displayed correctly in UILabel](http://stackoverflow.com/questions/5414730/custom-installed-font-not-displayed-correctly-in-uilabel) – The dude Sep 26 '13 at 11:02
  • Also check this out: http://stackoverflow.com/questions/7535498/uibutton-custom-font-vertical-alignment – The dude Sep 26 '13 at 11:16

2 Answers2

0

The correct values of NSTextAlignment are :

typedef enum _NSTextAlignment {
   NSLeftTextAlignment      = 0,
   NSRightTextAlignment     = 1,
   NSCenterTextAlignment    = 2,
   NSJustifiedTextAlignment = 3,
   NSNaturalTextAlignment   = 4
} NSTextAlignment;

NSTextAlignmentCenter is not a valid value.

Reference: https://developer.apple.com/library/mac/documentation/cocoa/reference/ApplicationKit/Classes/NSText_Class/Reference/Reference.html#//apple_ref/c/tdef/NSTextAlignment

Raptor
  • 53,206
  • 45
  • 230
  • 366
0

There may be two options for this: 1) One you can override drawText Method

- (void) drawTextInRect:(CGRect)rect
{   
    UIEdgeInsets insets = {0,5,0,5};

    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

2)You can use sizeToFit method to get exact height and width of UILabel.

Pankti Patel
  • 296
  • 1
  • 4
  • 14