Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView like in Mail.app reply view

In the iOS Mail app when you hit reply, you are presented with a text view like this. The vertical blue line keeps growing or shrinking (as the case may be) when you try to edit the previous reply while doing an inline reply or something. The top portion (your main reply) looks normal. Any idea on a really top level on how to pull this kind of text view off?

enter image description here

like image 437
Bourne Avatar asked Nov 13 '13 14:11

Bourne


1 Answers

enter image description here

    textViewHolder = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 280, 20)];
    simpleLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 2, 20)];
    simpleLine.backgroundColor = [UIColor blueColor];
    [textViewHolder addSubview:simpleLine];

    myTextView = [[UITextView alloc]initWithFrame:CGRectMake(20, 0, 240, 24)];
    myTextView.delegate = self;
    [textViewHolder addSubview:myTextView];
    [self.view addSubview:textViewHolder];


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
    int numLines = myTextView.contentSize.height / myTextView.font.lineHeight;
    if([text isEqualToString:@"\n"])
        numLines++;

    if(numLines>1)
    {
        CGRect frame = textViewHolder.frame;
        frame.size.height = 20*(numLines-1);
        textViewHolder.frame =frame;

        CGRect frame2 = simpleLine.frame;
        frame2.size.height = 20*(numLines-1);
        simpleLine.frame =frame2;

        CGRect frame3 = myTextView.frame;
        frame3.size.height = 24*(numLines-1);
        myTextView.frame =frame3;
    }
    return YES;
}

The (numLines-1) is because I always got one line more that I needed. I'm sure it's solvable with a bit debugging.

The [text isEqualToString:@"\n"] part is because you also want to increment the line number if the user presses done and going down a line.

like image 93
Segev Avatar answered Oct 31 '22 11:10

Segev