如何在ios中根据字符串动态调整UITextview高度

如何在ios中根据字符串动态调整UITextview高度,ios,iphone,objective-c,uitextview,Ios,Iphone,Objective C,Uitextview,我有一个简单的文本视图动态填充谁的数据。我想在填充数据后调整textview的高度,这样我就不会看到垂直滚动,也不会剪切文本。我想通过编程来完成此任务。我有一个标签,应该放在文本视图高度下20像素的位置,比如“感兴趣”。我正在尝试编写代码。但是我遇到了一些问题,比如对齐问题。如果我能运行程序,输出将显示如下 这是我的程序,请帮助我 lblHobbies = [[UILabel alloc]initWithFrame:CGRectMake(10, 310, 300, 20)]; lblHo

我有一个简单的
文本视图
动态填充谁的数据。我想在填充数据后调整textview的高度,这样我就不会看到垂直滚动,也不会剪切文本。我想通过编程来完成此任务。我有一个标签,应该放在文本视图高度下20像素的位置,比如“感兴趣”。我正在尝试编写代码。但是我遇到了一些问题,比如对齐问题。如果我能运行程序,输出将显示如下

这是我的程序,请帮助我

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

 lblHobbies.text=@"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)];

 lblInterests.text=@"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];

您可以使用自定义UITextView修复此问题:

请查看:

Textview将根据用户输入的字符串动态设置高度

希望这对你有帮助


:)

您可以使用自定义UITextView解决此问题:

请查看:

Textview将根据用户输入的字符串动态设置高度

希望这对你有帮助


:)

在此处输入代码您可以使用标签

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; myLabel.text=@“您的字符串”; CGSize labelSize=[myLabel.text sizeWithFont:myLabel.font constrainedToSize:CGSizeMake(320,您希望设置的最大高度) lineBreakMode:NSLineBreakByWordWrapping]; CGRect frame=myLabel.frame; frame.size.height=labelSize.height; myLabel.frame=帧;
在此处输入代码,您可以使用标签

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; myLabel.text=@“您的字符串”; CGSize labelSize=[myLabel.text sizeWithFont:myLabel.font constrainedToSize:CGSizeMake(320,您希望设置的最大高度) lineBreakMode:NSLineBreakByWordWrapping]; CGRect frame=myLabel.frame; frame.size.height=labelSize.height; myLabel.frame=帧; 我是这样做的:

+(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; 
}
将此方法传递给您的文本字体和框架。

以下是我的操作方法:

+(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; 
}

将此方法传递给您的文本字体和框架。

查看下面的代码希望对您有所帮助

  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)];
  lblHobbies.text=@"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)];
 lblInterests.text=@"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;


 }

通过下面的代码希望能帮助你

  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)];
  lblHobbies.text=@"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)];
 lblInterests.text=@"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;


 }

您只需将textView的高度更改为textView.contentSize.height的高度。执行此操作时,textView将更新并显示它包含的所有文本

textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width,textView.contentSize.height);
希望这对你有帮助。
快乐编码:)

您只需将textView的高度更改为textView.contentSize.height的高度即可。执行此操作时,textView将更新并显示它包含的所有文本

textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width,textView.contentSize.height);
希望这对你有帮助。
快乐编码:)

对于这两种支持,请看这同时支持iOS6和ios7。请看我的答案。希望您有所了解,只需使用该代码而不是您的代码……:)对于这两种支持,请看这同时支持iOS6和IOS7。请看我的答案。希望您有所了解,只需使用您的代码即可……:)感谢您的回复。我正在尝试运行您的代码。但我在这一行遇到异常“NSMutableAttributedString*消息=[[NSMutableAttributedString alloc]initWithString:str attributes:SystemFontAttributed];“有什么异常…!!”!!。。?它应该可以工作,等等,我会尝试清理和建立项目你从开始到结束都更换了吗?就像我做的那样,如果你仍然在单独的项目中尝试错误,只向xib添加scrollview,然后在-(void)viewDidLoad`方法和helper方法中复制并通过上述代码谢谢你的响应。我正在尝试运行你的代码。但是我得到了一个异常,这一行是“NSMutableAttributedString*message=[[NSMutableAttributedString alloc]initWithString:str attributes:systemFontAttrDict];“有什么异常!!。。?它应该可以工作,等等,我会尝试清理和建立项目你从开始到结束都更换了吗?就像我所做的那样,如果你仍然在单独的项目中尝试错误,只向xib添加scrollview,然后在-(void)viewDidLoad`方法和helper方法中复制并通过上面的代码