2

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

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

或者,还有其他方法可以解决这个问题吗?

谢谢克雷格

4

3 回答 3

2

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

label.layer.transform = CATransform3DMakeScale(desiredWidth/textWidth, 1.0, 1.0);
于 2012-10-05T15:57:48.943 回答
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];
    }
}
于 2012-04-10T11:10:20.327 回答
0

您可以将minimum font sizeUILabel 的 设置为较小的值,并检查Autoshrink以使其自动缩小。此参数在 Interface Builder 中可用。

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

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

于 2012-04-08T14:42:39.033 回答