iphone - 我在 ScrollView 上使用 UITextView ...并且我希望它在我在其中写入内容时自动展开......

标签 iphone

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/

相关文章:

ios - Apple 二进制文件被拒绝 (2.16)

iPhone UIView 动画

iphone - 如何使用 Xcode 将文本共享到 Tumblr

ios - 如何更改后退按钮上的文字

android - 用于包含所有 iPhone 和 Android 手机的最佳单个 CSS 媒体查询最小宽度是多少?

ios - Spritekit 游戏中的无尽滚动(重复)背景 - Swift

iphone - 使用 hitTest :? 获取 UIView 上的最低 View

iPhone App Store 更新系统,以及对数据库的影响

iphone - 下面的错误是什么意思?

ios - 如何在 prepareForSegue 函数中搜索数据时获取 indexPath