iphone - iOS——带有 UITextAlignmentLeft 的 UILabel 剪辑一个整数符号 ∫ 如果它是其文本的第一个字符

标签 iphone ios uilabel

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

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

标签中最左边的整数符号被剪掉了。

有没有一种干净的方法来解决这个问题?我的标签内容会因各种情况而频繁更改,所以我不想做一些骇人听闻的事情。

最佳答案

如果文本以整数符号开头,可以在开头加一个空格:

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

希望这对您有所帮助。

关于iphone - iOS——带有 UITextAlignmentLeft 的 UILabel 剪辑一个整数符号 ∫ 如果它是其文本的第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4218531/

相关文章:

iphone - 适用于 iPhone 的 MQTT 客户端

ios - UITableViewCell AccessoryView 显示不正确

ios - 有 2 行的 UILabel,如何独立截断每行?

iphone - 如何在 UILabel 上创建强烈的红色发光效果

ios - 旋转 UIView 后如何使边缘平滑

iphone - iOS4 - 快速上下文切换

iphone - 通知、委托(delegate)和协议(protocol)之间有什么区别?

ios - 在 iOS 应用程序中,主包和文档目录有什么区别?

ios - Spritekit 上双重生成(和重叠)敌人

ios - 识别标签中显示的属性字符串中的图像点击