james
james

Reputation: 643

How append string to a new line after setting exclusionPaths Rather than use NSTextAttachment?

How UITextView can append string to a new line like below screenshot show? Thanks for helping me.

Here is my code.

main code:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSTextStorage *storage = [NSTextStorage new];
    NSLayoutManager *layoutManager = [NSLayoutManager new];
    [storage addLayoutManager:layoutManager];
    NSTextContainer *textContainer = [NSTextContainer new];
    [layoutManager addTextContainer:textContainer];

    //setting exclusionPaths
    UIImage *image = [UIImage imageNamed:@"sample"];
    CGRect areaRect = CGRectMake(10, 50, 355, 180);
    UIBezierPath *ovalPath = [UIBezierPath bezierPathWithRect:areaRect];
    textContainer.exclusionPaths = @[ovalPath];

    //append text below image ,how to do?
    NSAttributedString *text  = [[NSAttributedString alloc]initWithString:@"\ntest\ntet2\ntet2\ntet2\ntet2"];
    [storage appendAttributedString:text];

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 375, 600) textContainer:textContainer];

    [self.view addSubview:textView];
    // add a image to exclusionPaths area
    [textView addSubview:showImageView];

}

enter image description here

Upvotes: 0

Views: 307

Answers (0)

Related Questions