gpt4 book ai didi

ios - UITextView 的换行文本(换行模式)

转载 作者:行者123 更新时间:2023-11-29 12:48:38 33 4
gpt4 key购买 nike

出现一个 UITextView(一种模式:按下按钮时显示),供用户收集他想在另一个固定的 UITextView 中写入的内容。我调整了 UITextView 的高度(固定高度),并将用户在另一个(模态)UITextView 中输入的文本放入其中。但是,固定的 UITextView 没有换行(就像在模态 UITextView 中所做的那样),而不是换行......如何让固定的 UITextView 包裹文本?

这是我的代码:

textsToDisplay[index] = [enterText text];

NSInteger _stringTotalLength=[[enterText text] length];
NSMutableAttributedString *attSt=[[NSMutableAttributedString alloc] initWithString:[enterText text]];
[attSt addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Helvetica" size:15.0]
range:NSMakeRange(0, _stringTotalLength)];

float height3 = ceil(attSt.size.height);

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
cellSizes[index] = CGSizeMake(screenWidth-6,height3+10);
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
cellSizes[index] = CGSizeMake(screenHeight-25,height3+10);
}

我将用户输入的文本存储在 textsToDisplay 中,这是一个 NSString 表。我使用的是 UICollectionView,其单元格包含固定的 UITextView,我在以下方法中设置文本:

- (UICollectionViewCell*) collectionView:(UICollectionView *)collectView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSMutableAttributedString *attSt=[[NSMutableAttributedString alloc] initWithString:textsToDisplay[index]];
NSInteger _stringTotalLength=[textsToDisplay[index] length];
[attSt addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Helvetica" size:15.0]
range:NSMakeRange(0, _stringTotalLength)];
cell.textView.attributedText =attSt;

我创建了 uitextview 来输入这样的代码:

float width = 500.0f;

enterText = [[UITextView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-(width)/2, self.view.frame.size.height/2-200, width, 400)];
NSMutableAttributedString *attSt=[[NSMutableAttributedString alloc] initWithString:questionCell.textView.text];
NSInteger _stringTotalLength=[questionCell.textView.text length];
[attSt addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Helvetica" size:15.0]
range:NSMakeRange(0, _stringTotalLength)];
enterText.attributedText = attSt;
enterText.tag = 92;
enterText.backgroundColor = [UIColor whiteColor];
enterText.alpha = 1;
enterText.delegate = self;
[self.view addSubview:enterText];

这是我在单元格中创建 uitextview 的方法:

 textView = [[UITextView alloc]initWithFrame:CGRectMake(50, 0, widthLabel-50, 50)];
textView.userInteractionEnabled = YES;
textView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
textView.backgroundColor = [UIColor clearColor];

我这样配置它:

cell.textView.frame = CGRectMake(50, 0, cellSizes[index].width-50, cellSizes[index].height);

最佳答案

我最终在每个 uicollectionviewcell 中使用了 uilabel,因为使用 uitextview 进行换行太复杂了。

关于ios - UITextView 的换行文本(换行模式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23014587/

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