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换行太复杂了。