开发者

iOS -- UILabel with UITextAlignmentLeft clips an integral sign ∫ if it is the first character of its text

The following is basically my entire project to illustrate the problem:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    UILabel *integralLabel = [[UILabel alloc]initWithFrame: CGRectMake(30, 60, 20开发者_开发问答0, 150)];
    integralLabel.font = [UIFont systemFontOfSize:100];
    [window addSubview:integralLabel];
    integralLabel.text = @"∫∫∫";
    integralLabel.textAlignment = UITextAlignmentLeft;
    integralLabel.backgroundColor = [UIColor yellowColor];
    [window makeKeyAndVisible];
    return YES;
}

The leftmost integral sign in the label is clipped.

Is there a clean way to fix this? The contents of my labels will change frequently in response to all sorts of things, so I don't want to do something hackish.


You can add a space at the beginning if the text begins with the integral sign:

if ([[integralLabel.text substringToIndex:1]isEqualToString:@"∫"])
{
    integralLabel.text = [NSString stringWithFormat:@" %@", integralLabel.text];
}

EDIT

To distinguish between this space and other spaces in the label, you can use different Unicode values for spaces (full list here: http://www.cs.tut.fi/~jkorpela/chars/spaces.html)

@"\u205F%@"

etc...

EDIT 2

You can also subclass UILabel by overriding drawTextInRect:.

- (void)drawTextInRect:(CGRect)rect
{
    if (![[self.text substringToIndex:1]isEqualToString:@"∫"])
    {
        [super drawTextInRect:rect];
    }
    else
    {
        CGRect paddingLeftRect = CGRectMake(rect.origin.x + 25.0, rect.origin.y, rect.size.width, rect.size.height);
        [super drawTextInRect:paddingLeftRect];
    }
}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜