iOS / UIFont - 减少字体宽度

时间:2012-04-08 14:37:14

标签: iphone ios cocoa-touch fonts uifont

我有一个固定大小的UILabel。不幸的是,在极少数情况下,我需要适合它的文字不适合!我试过减小字体大小,但它需要减少太多,看起来很糟糕。

是否可以以某种方式更改字体宽度? UIFont似乎没有任何属性允许我这样做?我是否需要使用UIWebView并使用CSS?我不太了解CSS,所以如果这是解决此问题的最佳方法,我们非常感谢。

或者,还有其他任何解决方法吗?

谢谢克雷格

3 个答案:

答案 0 :(得分:2)

缩小文本宽度的最简单方法是将变换应用于标签的图层:

label.layer.transform = CATransform3DMakeScale(desiredWidth/textWidth, 1.0, 1.0);

答案 1 :(得分:1)

你的意思是你想在保持高度的同时水平挤压它吗?这是可以实现的,高达常规宽度的约60%。除此之外它看起来很糟糕。

这是UILabel子类的drawRect,必要时可以在任一轴上独立挤压。

// This drawRect for a UILabel subclass reproduces most common UILabel formatting, but does not do truncation, line breaks, or scaling to fit.
// Instead, it identifies cases where the label text is too large on either axis, and shrinks along that axis.
// For small adjustments, this can keep text readable.  In extreme cases, it will create an ugly opaque block.
- (void) drawRect:(CGRect)rect;
{
    CGRect bounds = [self bounds];
    NSString *text = [self text];
    UIFont *font = [self font];

    // Find the space needed for all the text.
    CGSize textSize = [text sizeWithFont:font];

    // topLeft is the point from which the text will be drawn.  It may have to move due to compensate for scaling, or due to the chosen alignment.
    CGPoint topLeft = bounds.origin;

    // Default to no scaling.
    CGFloat scaleX = 1.0;
    CGFloat scaleY = 1.0;

    // If the text is too wide for its space, reduce it.
    // Remove the second half of this AND statement to have text scale WIDER than normal to fill the space.  Useless in most cases, but can be amusing.
    if ((textSize.width>0) && (bounds.size.width/textSize.width<1))
    {
        scaleX = bounds.size.width/textSize.width;
        topLeft.x /= scaleX;
    }
    else
    {
        // Alignment only matters if the label text doesn't already fill the space available.
        switch ([self textAlignment])
        {
            case UITextAlignmentLeft :
            {
                topLeft.x = bounds.origin.x;
            }
                break;

            case UITextAlignmentCenter :
            {
                topLeft.x = bounds.origin.x+(bounds.size.width-textSize.width)/2;
            }
                break;

            case UITextAlignmentRight :
            {
                topLeft.x = bounds.origin.x+bounds.size.width-textSize.width;
            }
                break;
        }
    }

    // Also adjust the height if necessary.
    if ((textSize.height>0) && (bounds.size.height/textSize.height<1))
    {
        scaleY = bounds.size.height/textSize.height;
        topLeft.y /= scaleY;
    }
    else
    {
        // If the label does not fill the height, center it vertically.
        // A common feature request is for labels that do top or bottom alignment.  If this is needed, add a property for vertical alignment, and obey it here.
        topLeft.y = bounds.origin.y+(bounds.size.height-textSize.height)/2;
    }

    // Having calculated the transformations needed, apply them here.
    // All drawing that follows will be scaled.
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(context, scaleX, scaleY);

    // Begin drawing.
    // UILabels may have a shadow.
    if ([self shadowColor])
    {
        [[self shadowColor] set];
        CGPoint shadowTopLeft = CGPointMake(topLeft.x+[self shadowOffset].width/scaleX, topLeft.y+[self shadowOffset].height/scaleY);
        [text drawAtPoint:shadowTopLeft withFont:font];
    }

    // The text color may change with highlighting.
    UIColor *currentTextColor;
    if ((![self isHighlighted]) || (![self highlightedTextColor]))
        currentTextColor = [self textColor];
    else
        currentTextColor = [self highlightedTextColor];

    // Finally, draw the regular text.
    if (currentTextColor)
    {
        [currentTextColor set];
        [text drawAtPoint:topLeft withFont:font];
    }
}

答案 2 :(得分:0)

您可以将UILabel的minimum font size设置为较小的值,然后选中Autoshrink让它自动缩小。此参数在Interface Builder中可用。

内部实现将减少字距,即字符之间的空格宽度。它实际上不能减小宽度。

这是你更好的选择。如果您仍对结果不满意。您可能需要更改设计。