0

5 つのランダムな文字列のうちの 1 つを表示する uilabel に少し問題があります。文字列全体が問題なく表示されることもありますが、多くの場合、テキストの一部が切り取られます。これが私が現在持っているものです:

// These are the 5 NSStrings which are placed in an array.
NSString *badOne = @"Bad? Your bill is nothing. I would consider that good service. SMDH.";
NSString *badTwo = @"You left out the amount dude.";
NSString *badThree = @"I can't tell you what the tip should be, if you don't tell me how much you dropped.";
NSString *badFour = @"Forgetting something?";
NSString *badFive = @"The tip should be over 9000... /n kidding.";

NSArray *badComments = [[NSArray alloc] initWithObjects:badOne, badTwo, badThree, badFour, badFive, nil];

// A random string is selected and assigned to badJoke.    
int rand = arc4random()%5;

NSString *badJoke = [badComments objectAtIndex:rand];

// Here's the problematic code:    
[_mainLabel setText:(badJoke)];
[_mainLabel setFrame:CGRectMake(50, 295, 200, 80)];
_mainLabel.adjustsFontSizeToFitWidth = NO;
_mainLabel.numberOfLines = 0;

[_amountTextField resignFirstResponder];

// This is a separate label, completely unrelated.    
[_tipAmount setText:@""];

たとえば、「Bad? Your bill is no」と表示されることがあります。また、 setFrame 行なしで試してみましたが、うまくいきませんでした。

どんな助けでも大歓迎です。ありがとう。

4

1 に答える 1

0

Ideal solution is to calculate the exact frame needed to display the text inside the lable...

NSString *badJoke = [badComments objectAtIndex:rand];

CGSize maximumSize = CGSizeMake(200, CGFLOAT_MAX);//you can give any max width which you feel that suits you...

UIFont *font = [UIFont systemFontOfSize:16];/Give your font size

CGSize size = [badJoke sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
//size.height will get you the total height of the lable to be created.

_mainLable=[[UILable alloc]initWithFrame:CGRectMake(50, 295, size.width, size.height)];
//if you haven't initialized.Or else you can use the same as you were using..[_mainLabel setFrame:CGRectMake(50, 295, size.width, size.height)];

_mainLabel.text= badJoke;

_mainLabel.lineBreakMode=NSLineBreakByWordWrapping;

_mainLabel.numberOfLines = 0;

I am not sure with the syntax,because i wrote the code on my notepad.Hope it works for you.Happy coding :)

于 2013-01-20T08:35:18.360 に答える