Haresh Ghatala
Haresh Ghatala

Reputation: 2006

Set Image inline with text to UITextView in Objective C

I want to add image inline with text to UITextView just like below screenshot

enter image description here


I have try using NSTextAttachment and NSAttributedString but it put only one line before & after the image.

Please help me to sort this out.

Upvotes: 0

Views: 960

Answers (1)

sanky009
sanky009

Reputation: 136

You need to just use 'UIBezierPath'

For example,

txtvHtmlString.text = @“your long text…….”;

imgView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 100, 152, 128)];

imgView.image = [UIImage imageNamed:@"smily.png"];     
imgView.layer.cornerRadius = 10;
[txtvHtmlString addSubview:imgView];

Then don't forget to update bezierpath in viewDidLayoutSubviews if your text is updated.

- (void)viewDidLayoutSubviews {
    UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:imgView.frame];
    txtvHtmlString.textContainer.exclusionPaths  = @[exclusionPath];
}

Upvotes: 2

Related Questions