2013-01-20 58 views
0

我對應該顯示1個隨機字符串的uilabel有點麻煩。有時整個字符串顯示沒有問題,但通常情況下,一些文本被切斷。這是我目前有:Objective-C:我的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:@""]; 

因此,舉例來說,一個時代可能出現「壞你的賬單沒有?」。 我也試過沒有setFrame線,沒有運氣。

任何幫助,將不勝感激。謝謝。

+0

當你增加UILabel框架時發生了什麼?它在變化嗎?如果你可以張貼你得到的結果的一些截圖, – iDev

+0

將會很有幫助 –

回答

0

的理想解決方案是計算顯示拉布勒內的文本所需的確切框架...

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; 

我不知道的語法,因爲我寫它的作品在我的notepad.Hope代碼爲你。快樂的編碼:)