textViewBusiness = [[UITextView alloc] initWithFrame:CGRectMake(25,332,268,60)];
textViewBusiness.text=strMyBusiness;
textViewBusiness.editable=NO;
textViewBusiness.font = [UIFont fontWithName:@"Arial" size: 17.0];
textViewBusiness.layer.borderWidth = 2.0f;
textViewBusiness.layer.borderColor = [[UIColor grayColor] CGColor];
textViewBusiness.backgroundColor = [UIColor clearColor];
[textViewBusiness setTextColor:[UIColor blackColor]];
[self.scrollView addSubview: textViewBusiness];
CGRect frame = textViewBusiness.frame;
frame.size.height = textViewBusiness.contentSize.height;
textViewBusiness.frame = frame;
随着内容的增加,我想增加文本字段的大小......
此代码对我不起作用...
谢谢
最佳答案
您不需要使用UITextView
来显示不可编辑的文本。除此之外,您可以使用 UILabel,您可以在运行时找出标签的高度。每次内容的大小变化都会相应地设置UILabel
的框架。
用它来找出运行时标签的高度
- (CGFloat) heightOfTextLabel:(NSString *) contentText
{
CGSize constraint = CGSizeMake(268,4000);
CGSize size = [contentText sizeWithFont:[UIFont fontWithName:@"Arial" size: 17.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
此方法每次都会返回内容的可变高度。
现在将此高度设置为您的UILabel
CGFloat heightOfLabel = [self heightOfTextLabel:strMyBusiness];
UILabel* textToShowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25,332,268,heightOfLabel)];
textToShowLabel.text=strMyBusiness;
textToShowLabel.font = [UIFont fontWithName:@"Arial" size: 17.0];
textToShowLabel.layer.borderWidth = 2.0f;
textToShowLabel.layer.borderColor = [[UIColor grayColor] CGColor];
textToShowLabel.backgroundColor = [UIColor clearColor];
[textToShowLabel setTextColor:[UIColor blackColor]];
[self.scrollView addSubview: textToShowLabel];
关于iphone - 我在 ScrollView 上使用 UITextView ...并且我希望它在我在其中写入内容时自动展开......,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11504211/