How to insert UIImageView in UITextView in the same way that iphone default message (SMS) app uses to insert multimedia content in the UITextView

后端 未结 4 1156
Happy的楠姐
Happy的楠姐 2021-01-18 21:16

I want to insert UIImageView in UITextView of Toolbar having send and camera button in the same way as iPhone default SMS app do.

相关标签:
4条回答
  • 2021-01-18 21:29

    You would be better off using a UIScrollView and managing UITextViews and UIImageViews in it. UITextView doesn't support adding image inline with text. In fact, it doesn't really support anything other than multiline text.

    Per your comment below, there are three things I can think of to get the image as part of the text entry box:

    1. They're not using a UITextView, but instead some custom view. That sort of thing is difficult to replicate.
    2. They are overlaying a UIImageView over the UITextView as a subview and setting the contentInset of the UITextView so there is no overlap.
    3. They are using a separate UIView to contain both the UITextView and UIImageView as subviews and simply arrange those subviews as needed.

    Both 2 & 3 are very similar (just slightly different approaches) and probably your best approach. Personally, I think 3 is probably the best, since it give you the most control over the position of both views, but 2 should also work fine.

    0 讨论(0)
  • 2021-01-18 21:31

    I agree with Aaron. Based on what I have seen, I believe the native SMS app is actually a UITableView with highly modified TableCells. The TableCells are then composite views that contain the UITextView and UIImageView as Aaron suggested.

    It might be a little more work up front, but I think you will find the customization of defining your own UITableCell with the above elements will be quite useful and fall in line with the overall iOS paradigm. Things work a lot better when you work with the native paradigms than against / around them.

    Cheers

    0 讨论(0)
  • 2021-01-18 21:33

    You can implement it using UITextViewDelegate and ContentInset.

     - (void)viewDidLoad 
     {
        self.textView.delegate = self;
        [self.textView addSubview:self.addedView];
        [self.textView setContentInset:UIEdgeInsetsMake(0, 0, CGRectGetHeight(self.addedView.frame), 0)];
     }
    
    - (void)textViewDidChange:(UITextView *)textView
     {
        NSLog(@"%@",NSStringFromCGSize(textView.contentSize));
        __weak typeof(self) wself = self;
        [UIView animateWithDuration:0.5f animations:^{
          [wself.addedView setFrame:CGRectMake(0, textView.contentSize.height + 10, CGRectGetWidth(wself.addedView.frame), CGRectGetHeight(wself.addedView.frame))];
        }];
     }
    
    0 讨论(0)
  • 2021-01-18 21:53

    I have one suggetion that try to make html file with image and text as per your requirements and load that html file into webview.

    Here you can also back some particular text Bold etc.

    I think nice look then textfield.

    To make webview just look like simple scroll view just put this method in your code

    don't forgot to write this

    webView.opaque = NO;,

    [self hideGradientBackground:webView]; and

    - (void) hideGradientBackground:(UIView*)theView
    {
        for (UIView * subview in theView.subviews)
        {
            subview.backgroundColor = [UIColor clearColor];
            if ([subview isKindOfClass:[UIImageView class]])
                subview.hidden = YES;
    
            [self hideGradientBackground:subview];
        }
    }
    

    I hope this may help you.

    0 讨论(0)
提交回复
热议问题