0

我在我的应用程序中使用自定义字体。我已将其设置为标签、按钮和文本字段。但是当我使用自定义字体时,所有对象的文本对齐方式都会改变。它从顶部显示。

这是带有默认字体的标签:

在此处输入图像描述

这是带有自定义字体的标签:

在此处输入图像描述

这是代码:

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

任何指针?

4

2 回答 2

0

的正确值为NSTextAlignment

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

NSTextAlignmentCenter不是有效值。

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

于 2013-09-25T10:18:02.780 回答
0

可能有两种选择: 1)您可以覆盖 drawText 方法

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

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

2)您可以使用sizeToFit方法来获取UILabel的确切高度和宽度。

于 2013-09-26T10:25:10.730 回答