包装UITextView的文本(换行模式)

时间:2014-04-11 13:52:56

标签: ios uitextview uicollectionview

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);

1 个答案:

答案 0 :(得分:1)

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

相关问题