gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:03:22 29 4
gpt4 key购买 nike

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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com