Search code examples
iosobjective-cuitextviewnsattributedstring

Duplicated UITextView doesn't show all text from the original


I'm trying to make a duplicate UITextView based on another UITextView for sharing purposes. This text view is added as a subview to viewToShare. There is an issue however, the duplicated text view doesn't show all of the original text from the text view I'm copying from. I'm using attributed text as well on the original text view, so I'm not sure if this is the issue. Setting the background color to black on textViewCopy showed me that the frame size is correct. For some reason it seems like new line \n characters from the original text view are causing havoc and preventing the text to be fully shown in the textViewCopy. I wonder if it's related to this question: NSAttributedString '\n' ignored

Screenshots:

enter image description hereenter image description here

Code:

- (UIView *)shareView
{
    CGSize size = self.containerView.bounds.size;

    UIView *viewToShare = [[UIView alloc]init];
    viewToShare.backgroundColor = self.containerView.backgroundColor;
    viewToShare.layer.cornerRadius = 6.0;
    viewToShare.layer.masksToBounds = YES;

    UITextView *textViewCopy = [[UITextView alloc]init];
    textViewCopy.backgroundColor = [UIColor clearColor];
    textViewCopy.tag = 1;

    UIEdgeInsets textContainerInsets = self.textView.textContainerInset;

    viewToShare.frame = CGRectMake(0, 0, size.width, size.height);
    textViewCopy.frame = CGRectMake(0, 0, size.width, size.height);

    textViewCopy.textContainerInset = textContainerInsets;

    NSAttributedString *attributedStringCopy = [[NSAttributedString alloc]
    initWithAttributedString:self.textView.attributedText];

    textViewCopy.attributedText = attributedStringCopy;

    [viewToShare addSubview:textViewCopy];

    return viewToShare;
}

Solution

  • I figured out how to solve the issue based on this SO: Large Text Being Cut Off in UITextView That is Inside UIScrollView

    - (UIView *)shareView
    {
        CGSize size = self.containerView.bounds.size;
    
        UIView *viewToShare = [[UIView alloc]init];
        viewToShare.backgroundColor = self.containerView.backgroundColor;
        viewToShare.layer.cornerRadius = 6.0;
        viewToShare.layer.masksToBounds = YES;
    
        UITextView *textViewCopy = [[UITextView alloc]init];
        textViewCopy.backgroundColor = [UIColor clearColor];
        textViewCopy.tag = 1;
    
        UIEdgeInsets textContainerInsets = self.textView.textContainerInset;
    
        viewToShare.frame = CGRectMake(0, 0, size.width, size.height);
        textViewCopy.frame = CGRectMake(0, 0, size.width, size.height);
    
        // These two lines are needed to fix bug!
        textViewCopy.scrollEnabled = NO;
        textViewCopy.scrollEnabled = YES;
    
        textViewCopy.textContainerInset = textContainerInsets;
    
        NSAttributedString *attributedStringCopy = [[NSAttributedString alloc]
        initWithAttributedString:self.textView.attributedText];
    
        textViewCopy.attributedText = attributedStringCopy;
    
        [viewToShare addSubview:textViewCopy];
    
        return viewToShare;
    }