重复的 UITextView 不显示原始文本中的所有文本

Duplicated UITextView doesn't show all text from the original

我正在尝试根据另一个 UITextView 制作一个副本 UITextView 以供共享。此文本视图作为子视图添加到 viewToShare。但是有一个问题,复制的文本视图不显示我正在复制的文本视图中的所有原始文本。我也在原始文本视图中使用属性文本,所以我不确定这是否是问题所在。在 textViewCopy 上将背景颜色设置为黑色表明帧大小是正确的。出于某种原因,原始文本视图中的新行 \n 字符似乎正在造成破坏并阻止文本在 textViewCopy 中完全显示。我想知道是否与这个问题有关:NSAttributedString '\n' ignored

截图:

代码:

- (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;
}

我想出了如何根据这个 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;
}