Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top