3

次の2つの方法(1つはのカテゴリ、もう1つはのカテゴリNSStringUILabelを使用して、ラベル内のテキストに基づいてラベルの高さを自動的に調整しています。ほとんどの場合は正常に機能していますが、予測できない結果が生じています。問題がどこで発生しているのかよくわかりません。すばらしい人々が助けてくれることを願っています。まず、問題のメソッドは次のとおりです。

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ピクセルある可能性がありますが、6行の文字列はほぼ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 に答える