4

この回答で提供されているコードを使用して動的ラベルを作成していますが、ほとんどの場合機能します。ただし、ラベルテキストの長さが94文字を超えると、切り捨てられて省略記号が追加されます。

これについてもう1つ奇妙なことは、文字列にさらに文字を追加すると表示されますが、最後の2行はまだ切り捨てられていることです。

例えば。

文字列:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one two three.

このように表示されます:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one tw...

しかし、ラベルで同じ文を再度使用して文字列を2倍にすると、テキストがより多く表示されますが、それでも切り捨てられます。

例えば。

文字列:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one two 
three. this is a very very long 
string with lots of words to test 
the dynamic bubble sizing one 
two three.

このようなショー:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one two 
three. this is a very very long 
string with lots of words to tes...

これが私が使用しているコードです。

NSString *temp = [NSString stringWithFormat:@"this is a very very long string with lots of words to test the dynamic bubble sizing one two three"];

captionLabel.text = temp;
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);

CGSize expectedLabelSize = [temp sizeWithFont:captionLabel.font 
                                  constrainedToSize:maximumLabelSize 
                                      lineBreakMode:captionLabel.lineBreakMode]; 

//adjust the label the the new height.
CGRect newFrame = captionLabel.frame;
newFrame.size.height = expectedLabelSize.height;
captionLabel.frame = newFrame; 

これは私が私の頭を掻くので、誰かがアイデアを持っていることを願っています。

編集

ハードコードされた296の代わりにcaptionLabel.frame.size.widthを使用すると、@ trooleeのおかげで修正されました。彼/彼女が回答を作成することを選択した場合、私はそれを正しいとマークします。

4

4 に答える 4

1
 NSString * test=@"this is test this is test inthis is test ininthis is test inthis is test inthis is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel END";

        testLabel.text = test;
        testLabel.numberOfLines = 0; //will wrap text in new line
        [testLabel sizeToFit];

        [self.view addSubview:testLabel];

これはきっとあなたを助けるでしょう。

ありがとう

于 2012-07-19T10:38:27.220 に答える
1

の代わりにcaptionLabel.lineBreakMode、と書くだけUILineBreakModeWordWrapです。動作するはずです。

于 2012-07-19T10:49:01.453 に答える
1

@trooleeが今までに彼のコメントに答えてくれることを期待していましたが、彼が答えを投稿していないので、この質問を締めくくることができるように、答えを投稿して正しいマークを付けます。

ハードコードされた296の代わりにcaptionLabel.frame.size.widthを使用すると修正されました。

于 2012-08-10T11:26:00.970 に答える
0

UILabel次のカテゴリを試してください。作成者に感謝します。

UILabel + VAlign.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface UILabel (VAlign)
- (void) setVerticalAlignmentTopConstrainedToSize:(CGSize)size;
@end

UILabel + VAlign.h

#import "UILabel+VAlign.h"

@implementation UILabel (VAlign)

- (void) setVerticalAlignmentTopConstrainedToSize:(CGSize)size
{
    CGSize textSize = [self.text sizeWithFont:self.font
                            constrainedToSize:size
                                lineBreakMode:self.lineBreakMode];

    CGRect textRect = CGRectMake(self.frame.origin.x,
                                 self.frame.origin.y,
                                 self.frame.size.width,
                                 textSize.height);
    [self setFrame:textRect];
    [self setNeedsDisplay];
}

@end
于 2012-07-19T10:46:03.803 に答える