2014-02-24 45 views
1

我有一個簡單的textView誰動態填充了數據。我想調整textview的高度,一旦填充數據,以便我看不到垂直滾動或文本被剪輯。我想以編程方式執行此任務。我有一個標籤,應該被放置在文本視圖的高度低於20像素像「有趣」。我試圖做代碼。但我有一些問題,如對齊問題。如果我可以運行該程序,輸出將顯示像這樣。如何在ios中動態調整基於字符串的UITextview高度

enter image description here

這是我Program.please幫我任何人。

lblHobbies = [[UILabel alloc]initWithFrame:CGRectMake(10, 310, 300, 20)]; 

[email protected]"Hobbies"; 

lblHobbies.font=[UIFont systemFontOfSize:16.0]; 

lblHobbies.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1]; 

[scrollView addSubview:lblHobbies]; 

lblInterests = [[UILabel alloc]initWithFrame:CGRectMake(10, 420, 300, strSize.height+30)]; 

[email protected]"Interests"; 

lblInterests.font=[UIFont systemFontOfSize:16.0]; 

lblInterests.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1]; 

[scrollView addSubview:lblInterests]; 

NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14]}; 

CGRect rect = 
     [tViewhobbies.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) 
             options:NSStringDrawingUsesLineFragmentOrigin 
             attributes:attributes 
             context:nil]; 

tViewhobbies=[[UITextView alloc]init]; 

tViewhobbies.frame=CGRectMake(10, 330,300, rect.size.height); 

[tViewhobbies setUserInteractionEnabled:NO]; 

tViewhobbies.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5]; 

tViewhobbies.delegate=self; 

[scrollView addSubview:tViewhobbies]; 

NSDictionary *attributes1 = @{NSFontAttributeName: [UIFont systemFontOfSize:14]}; 

CGRect rect1 = 
     [tViewInterests.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) 
              options:NSStringDrawingUsesLineFragmentOrigin 
             attributes:attributes1 
              context:nil]; 

tViewInterests=[[UITextView alloc]init]; 

tViewInterests.frame=CGRectMake(10, 450, 300, rect1.size.height); 

[tViewInterests setUserInteractionEnabled:NO]; 

tViewInterests.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5]; 

tViewInterests.delegate=self; 

[scrollView addSubview:tViewInterests]; 
+2

這兩個支持看到這個http://stackoverflow.com/questions/19215199/dynamically-size-uitableviewcell-according-to-uilabel-with-paragraph-spacing/19217965#19217965支持iOS6和iOS7 –

+0

乾草看到我的答案希望你有一些想法,只是使用你的代碼insted你的:) :) –

回答

4

經過下面的代碼,希望可以幫助ü

self.scrollView.contentSize = CGSizeMake(320, 700); 
    UILabel *lblHobbies = [[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + 2, 75, 40)]; 
    [email protected]"Hobbies"; 
    lblHobbies.font=[UIFont systemFontOfSize:16.0]; 
    lblHobbies.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1]; 

    [self.scrollView addSubview:lblHobbies]; 


    //add this after the hobbies textview so u need to calculate the height, so wy dont you put some helper method that returns the height for the text 
    //i took some long text that u want to display 
    NSString *str = @"BARCELONA, Spain -- Nokia is targeting emerging markets with three low-cost smartphones that use Google's Android operating system rather than the Windows Phone software from Microsoft, which is about to buy Nokia's phone business"; 


    CGSize hobbiesTextViewSize = [self calculateHeightForString:str]; 
    //now set the hobbiesTextView frame and also the text 
    UITextView *tViewhobbies = [[UITextView alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + 40, hobbiesTextViewSize.width, hobbiesTextViewSize.height+ 5)]; 
    tViewhobbies.font = [UIFont systemFontOfSize:17.0f]; 
    tViewhobbies.text = str;//set the text hear 
    [self.scrollView addSubview:tViewhobbies]; 

    //now add the lblInterests label 
    UILabel *lblInterests = [[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + lblHobbies.frame.size.height + hobbiesTextViewSize.height + 2, 75, 40)]; 
[email protected]"Interests"; 

lblInterests.font=[UIFont systemFontOfSize:16.0]; 

lblInterests.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1]; 
[self.scrollView addSubview:lblInterests]; 


NSString *str1 = @"Nokia will ditch many of the Google services that come with Android, which Google lets phone makers customize at will. Instead, the new Nokia X line announced Monday will emphasize Microsoft services such as Bing search, Skype communications and OneDrive file storage. Its home screen sports larger, resizable tiles resembling those on Windows phone."; 

    //now calculate the height for Interests text 
    CGSize interestTextViewSize = [self calculateHeightForString:str1]; 
    UITextView *tViewInterests = [[UITextView alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + lblHobbies.frame.size.height + lblInterests.frame.size.height + tViewhobbies.frame.size.height + 4, interestTextViewSize.width + 2, interestTextViewSize.height+ 2)]; 
    tViewInterests.font = [UIFont systemFontOfSize:17.0f]; 
    tViewInterests.text = str1; 
    [self.scrollView addSubview:tViewInterests]; 



} 

//our helper method 
- (CGSize)calculateHeightForString:(NSString *)str 
{ 
    CGSize size = CGSizeZero; 

UIFont *labelFont = [UIFont systemFontOfSize:17.0f]; 
NSDictionary *systemFontAttrDict = [NSDictionary dictionaryWithObject:labelFont forKey:NSFontAttributeName]; 

NSMutableAttributedString *message = [[NSMutableAttributedString alloc] initWithString:str attributes:systemFontAttrDict]; 
CGRect rect = [message boundingRectWithSize:(CGSize){320, MAXFLOAT} 
              options:NSStringDrawingUsesLineFragmentOrigin 
              context:nil];//you need to specify the some width, height will be calculated 
size = CGSizeMake(rect.size.width, rect.size.height + 5); //padding 

return size; 


} 
+0

感謝您的response.I試圖運行您的code.But我得到異常這一行「NSMutableAttributedString * message = [ [NSMutableAttributedString alloc] initWithString:str attributes:systemFontAttrDict];「 – user3222991

+0

什麼是例外...... !! ..?它應該工作等待,我會嘗試 –

+0

清潔和建設項目 –

0

輸入代碼hereYou可以使用標籤。

 
myLabel.text = @"Your String"; 
CGSize labelSize = [myLabel.text sizeWithFont:myLabel.font 
          constrainedToSize:CGSizeMake(320,MAX_HEIGHT_YOU_WANT_TO_SET) 
           lineBreakMode:NSLineBreakByWordWrapping]; 
CGRect frame = myLabel.frame; 
frame.size.height = labelSize.height; 
myLabel.frame = frame; 

1

這是我如何做的:

+(CGFloat)getLabelDymanicHeightOfStringWithText:(NSString *)text andFont:(UIFont *)font andFrame:(CGRect)frame { 
    CGSize maxSize = CGSizeMake(frame.size.width, 999999.0); 
    int height = 0; 

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
             font, NSFontAttributeName, 
             nil]; 

if (IS_IOS_6)//iOS 6 macro 
{ 
    CGSize size = [text sizeWithFont:font 
          constrainedToSize:maxSize 
           lineBreakMode:NSLineBreakByWordWrapping]; 
    height = size.height; 
} 
else 
{ 
    CGRect frame = [text boundingRectWithSize:maxSize 
              options:NSStringDrawingUsesLineFragmentOrigin 
              attributes:attributesDictionary 
              context:nil]; 
    height = frame.size.height; 
} 

return height+5; 
} 

通過這個方法,你的文字文本字體和框架。

0

,你可以簡單地改變TextView的高度到textView.contentSize.height的高度。這樣做你textView將更新並顯示它包含的所有文本。

textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width,textView.contentSize.height); 

希望這可以幫助你。 快樂編碼:)