iOS - UILabel 与 UITextAlignmentLeft 剪辑积分符号 ∫如果它是文本的第一个字符

发布于 2024-10-03 01:34:23 字数 636 浏览 1 评论 0原文

以下基本上是我的整个项目来说明问题:

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

标签中最左边的积分符号被剪掉。

有没有一种干净的方法来解决这个问题?我的标签内容会根据各种各样的事情而经常改变,所以我不想做一些黑客的事情。

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, 200, 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此生挚爱伱 2024-10-10 01:34:24

如果文本以积分符号开头,您可以在开头添加一个空格:

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

编辑

要区分此空格和标签中的其他空格,您可以对空格使用不同的 Unicode 值(完整列表如下: http://www.cs.tut.fi/~jkorpela/chars/spaces .html)

@"\u205F%@"

等...

编辑2

您还可以通过重写drawTextInRect:来子类化UILabel。

- (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];
    }
}

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文