3

我正在使用以下两种方法(一种是 的类别,NSString另一种是 的类别UILabel)根据其中的文本自动调整标签的高度。它在大多数情况下运行良好,但会产生一些不可预测的结果。我不太确定问题可能出在哪里,我希望你们中的一些人能提供帮助。首先,这是有问题的方法:

NSString 类别:

- (CGFloat) fontSizeWithFont: (UIFont *) font constrainedToSize: (CGSize) size minimumFontSize: (CGFloat) minimumFontSize
{
    CGFloat fontSize = [font pointSize];
    CGFloat height = [self sizeWithFont: font constrainedToSize: CGSizeMake(size.width, FLT_MAX) lineBreakMode: NSLineBreakByWordWrapping].height;
    UIFont *newFont = font;

    // Reduce font size while too large, break if no height (empty string)
    while (height > size.height && height != 0 && fontSize > minimumFontSize) {
        fontSize--;
        newFont = [UIFont fontWithName: font.fontName size: fontSize];
        height = [self sizeWithFont: newFont constrainedToSize: CGSizeMake(size.width, FLT_MAX) lineBreakMode: NSLineBreakByWordWrapping].height;
    };

    // Loop through words in string and resize to fit
    for (NSString *word in [self componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]) {
        CGFloat width = [word sizeWithFont: newFont].width;
        while (width > size.width && width != 0 && fontSize > minimumFontSize) {
            fontSize--;
            newFont = [UIFont fontWithName: font.fontName size: fontSize];
            width = [word sizeWithFont: newFont].width;
        }
    }
    return fontSize;
}

UIL标签类别:

- (void) sizeToFitMultipleLines
{
    if (self.adjustsFontSizeToFitWidth) {
        CGFloat adjustedFontSize = [self.text fontSizeWithFont: self.font constrainedToSize: self.frame.size minimumFontSize: self.minimumScaleFactor];
        self.font = [self.font fontWithSize: adjustedFontSize];
    }
    [self sizeToFit];
}

发生的问题是标签的高度在其顶部和底部都有空白空间,并且其中的字符串越长,空白空间越大。在标签的框架内,单行标签可能在其上方和下方有 10 个像素,但六行字符串可能有近 100 个像素。在上述方法中,我无法追踪这些额外空间的来源。

其次,标签的宽度有时会在我只想改变高度的地方进行调整。我猜这[self sizeToFit]就是导致这个特定问题的原因,但我不完全确定,因为如果我移除它,高度仍然没有调整。编辑:我已经通过在调整大小以适应标签后手动重新定位标签来解决此宽度问题(它的 X 坐标现在是屏幕宽度减去标签宽度,除以 2)。

有什么想法吗?如果您需要其他信息或代码,请询问。我总是在调用相关[sizeToFitMultipleLines]标签上的方法之前设置字体,所以这不是问题。

4

1 回答 1

6

我已经尝试过了,它对我有用。希望这可以帮助你一点。

-(void) getDynamicHeightWithString:(NSString*)labelText forWidth:(CGFloat)lblWidth forPoint:(CGPoint)point
{
    //Create Label
    UILabel *label = [[UILabel alloc] init];
    label.text = labelText;
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;

    //Set frame according to string
    CGSize size = [label.text sizeWithFont:label.font
                         constrainedToSize:CGSizeMake(lblWidth, MAXFLOAT)
                             lineBreakMode:UILineBreakModeWordWrap];
    [label setFrame:CGRectMake(point.x , point.y , size.width , size.height)];

    //Add Label in current view
    [self.view addSubview:label];    

}

利用

NSString *labelText = @"We are what our thoughts have made us; so take care about what you think. Words are secondary. Thoughts live; they travel far.The issue that's occurring is that the height of the label has empty space at its top and bottom, and that the longer the string inside it is, the larger that empty space is. A single line label might have perhaps 10 pixels above and below it inside the label's frame, but a six-line string may have almost 100 pixels. I'm not able to track down where this extra space is coming from in the methods above.";

[self getDynamicHeightWithString:labelText forWidth:200 forPoint:CGPointMake(15, 15)]; 
于 2013-03-07T13:12:21.553 回答