7

UILabel を使用して可変高さのテキストを表示しようとしています。
Xcode のストーリーボードとコーディングを組み合わせて、すべてを実装ファイルに直接セットアップしました。

コードは次のとおりです。

CGRect labelFrame = CGRectMake(20, 20, 280, 800);
descriptionLabel.frame = labelFrame;
NSString *description = self.spell[@"description"];
descriptionLabel.text = description;
[descriptionLabel setNumberOfLines:0];
[descriptionLabel sizeToFit];

wordwrap 関数を変更して、さまざまな設定を試してみましたが、役に立ちませんでした。

ストーリーボードで行う必要がある特定のことはありますか?
view->label の構造は重要ですか?
ラベルがビューに表示されるようにしています(画面全体を占めます)。

4

6 に答える 6

10

高さを手動で計算する必要はありません。可変高さの場合、次sizeToFitのように、を呼び出す前にラベルの高さを 0 に設定します。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
label.numberOfLines = 0;
label.text = @"Some long piece of text...";

[label sizeToFit];
于 2013-12-09T04:10:10.893 に答える
1

あなたの問題(動的高さ)などの問題に対する私の解決策は次のとおりです。

NSString *description = @"Some text";

CGSize dynamicSize = [[description sizeWithFont:[UIFont fontWithName:@"FONT_NAME" size:14] constrainedToSize:CGSizeMake(300, 10000)]; //the width should be the one you need and just put in a very big height so that anything you would put in here would fit
//dynamicSize is now the exact size you will need, with the fixed width and the height just enough so that all the text will fit.

UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake:(0, 0, dynamicSize.width, dynamicSize.height)];
someLabel.font = YOUR_FONT;
someLabel.numberOfRows = 10 //You should calculate how many rows you need by dividing dynamicSize.height to the height of one row (should also take into account the padding that your UILabel will put between rows

私の意見では、これが必要な高さを決定する最も安全な方法です。また、私の知る限り、 sizeToFit を呼び出しても、すべてのテキストを保持するのに十分な大きさが既にある場合、サイズは変更されません (小さくはなりません...必要に応じて大きくなりますが、行は追加されません)

于 2013-02-07T15:20:49.290 に答える
0

lineBreakMode を設定しているようには見えません。

私はスタックオーバーフローからいくつかのコードを取得し、それをカテゴリに入れました

ただし、コードだけが必要な場合は、次のとおりです。

@interface UILabel (BPExtensions)
- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth;
@end
 
@implementation UILabel (EnkiExtensions)
- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth
{
    if (fixedWidth < 0) {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 0);
    } else {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);
    }
    self.lineBreakMode = UILineBreakModeWordWrap;
    self.numberOfLines = 0;
    [self sizeToFit];
}
@end

次のように呼び出します。

helpLabel_.text = @"You need to cut the blue wire and ground the red wire. Do not cut the red wire and ground the blue wire, that would be bad.";
[helpLabel_ sizeToFitFixedWidth: desiredWidth ];
于 2013-02-07T15:27:03.733 に答える
0

私は同じものを作成しようとしましたが、それは私と完全にうまく機能します。(アプリにストーリーボードはありませんが、何の影響もないと思います。)

UILabel *bioLabel = [[UILabel alloc] init];
CGRect labelFrame = CGRectMake(0, 100, 320, 500);
bioLabel.frame = labelFrame;
NSString *bio =@"YOUR_TEXT_HERE";
bioLabel.text = description;
[bioLabel setNumberOfLines:0];
[bioLabel sizeToFit];
[self.view addSubview:bioLabel];
于 2013-02-07T13:29:06.857 に答える