代码之家  ›  专栏  ›  技术社区  ›  William Jockusch

iOS-uiLabel与uitexAlignmentLeft如果是其文本的第一个字符,则会剪辑一个整型符号。

  •  1
  • William Jockusch  · 技术社区  · 14 年前

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

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

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

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

    1 回复  |  直到 11 年前
        1
  •  0
  •   Evan Mulawski    11 年前

    如果文本以整型符号开头,则可以在开头添加空格:

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

    编辑

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

    @"\u205F%@"
    

    等。。。

    编辑2

    您还可以通过重写uilabel子类 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];
        }
    }
    

    希望这有帮助。