如何更改UILabel/UIFont的字母间距?

And*_*dre 38 iphone xcode spacing uilabel uifont

我已经搜索了负载,但找不到答案.

我有一个正常的UILabel,这样定义:

    UILabel *totalColors = [[[UILabel alloc] initWithFrame:CGRectMake(5, 7, 120, 69)] autorelease];
    totalColors.text = [NSString stringWithFormat:@"%d", total];
    totalColors.font = [UIFont fontWithName:@"Arial-BoldMT" size:60];
    totalColors.textColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
    totalColors.backgroundColor = [UIColor clearColor];
    [self addSubview:totalColors];
Run Code Online (Sandbox Code Playgroud)

我希望字母之间的水平间距更紧,同时保持字体大小.

有没有办法做到这一点?这应该是一件非常基本的事情.

干杯,安德烈

更新:

所以我被迫这样做:

- (void) drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSelectFont (context, "Arial-BoldMT", 60, kCGEncodingMacRoman);
    CGContextSetCharacterSpacing (context, -10);
    CGContextSetTextDrawingMode (context, kCGTextFill);

    CGContextSetRGBFillColor(context, 221/255.0, 221/255.0, 221/255.0, 221/255.0);
    CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
    CGContextSetTextMatrix(context, xform);

    char* result = malloc(17);
    sprintf(result, "%d", totalNumber);

    CGContextShowTextAtPoint (context, 0, 54, result, strlen(result));
}
Run Code Online (Sandbox Code Playgroud)

但我需要将其与右对齐.如果我知道绘制文本的宽度,我可以手动完成,但事实证明几乎不可能找到它.

我读过ATSU,但我找不到任何例子.

这太烂了:/

Kri*_*zai 43

从iOS 6开始,您可以在UILabel中使用NSAttributedString.

在属性字符串中,您可以使用属性NSKernAttributeName来设置字母间距

NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString: @"Test test test test "];
[attrStr addAttribute:NSKernAttributeName value:@(4.0) range:NSMakeRange(0, attrStr.length)];

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 300, 100)];
label.attributedText = attrStr;
Run Code Online (Sandbox Code Playgroud)


use*_*349 11

我扩展了UILabel来改变字符间距.这应该可以解决方案并从UILabel本身中提取字体,文本,颜色等(正确编码!).

你可能会注意到我画了两次文字,首先是清晰的颜色.这是为了使标签中的文本自动居中.虽然这可能效率低下 - 以自动为中心不是很好吗?

请享用!

@interface RALabel : UILabel {
}
@end  

@implementation RALabel 

- (void) drawRect:(CGRect)rect 
{

    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingMacRoman);
    CGContextSetCharacterSpacing(context, 1);
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f );
    CGContextSetTextMatrix (context, myTextTransform);

    // draw 1 but invisbly to get the string length.
    CGPoint p =CGContextGetTextPosition(context);
    float centeredY = (self.font.pointSize + (self.frame.size.height- self.font.pointSize)/2)-2;
    CGContextShowTextAtPoint(context, 0, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]);
    CGPoint v =CGContextGetTextPosition(context);

    // calculate width and draw second one.
    float width = v.x - p.x;
    float centeredX =(self.frame.size.width- width)/2;
    CGContextSetFillColorWithColor(context, [self.textColor CGColor]);
    CGContextShowTextAtPoint(context, centeredX, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]);

}
Run Code Online (Sandbox Code Playgroud)

  • 您只能在此解决方案中使用 ascii 字符 (2认同)

And*_*dre 8

我想出了一个字母间距和右边对齐的解决方案.

在这里:

    NSString *number = [NSString stringWithFormat:@"%d", total];

    int lastPos = 85;

    NSUInteger i;
    for (i = number.length; i > 0; i--)
    {
        NSRange range = {i-1,1};
        NSString *n = [number substringWithRange:range];

        UILabel *digit = [[[UILabel alloc] initWithFrame:CGRectMake(5, 10, 35, 50)] autorelease];
        digit.text = n;
        digit.font = [UIFont fontWithName:@"Arial-BoldMT" size:60];
        digit.textColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
        digit.backgroundColor = [UIColor clearColor];
        [self addSubview:digit];

        CGSize textSize = [[digit text] sizeWithFont:[digit font]];
        CGFloat textWidth = textSize.width;

        CGRect rect = digit.frame;
        rect.origin.x = lastPos - textWidth;
        digit.frame = rect;

        lastPos = rect.origin.x + 10;
    }
Run Code Online (Sandbox Code Playgroud)

字母间距是最后一行的"10".对齐来自lastPos.

希望这可以帮助那里的任何人.

  • 你的方法似乎是合情合理的(也是第一个出现在我脑海中的方法)但不幸的是,如果需要渲染许多文本区域,它可能效果不佳.我认为安德烈提供的更新在一般情况下要好得多 (3认同)