簡體   English   中英

自動調整UILabel的高度

[英]Automatically adjusting height of a UILabel

我正在使用以下兩種方法(一種是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;
}

UILabel類別:

- (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]方法之前總是設置字體,所以這不是問題。

我試過這個,它為我工作。 希望這可以幫助你一點。

-(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)]; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM