evenodd
evenodd

Reputation: 2126

How could you make a uilabel wrap around an image (like shown)

How could you achieve this effect:enter image description here

Maybe some sort of NSAtributedString?

I have thought of hacky ways to just add spaces, but it needs to do it dynamically based on the width of the image.

Any ideas?


NOTE happily you can do this very easily with UITextView:

https://stackoverflow.com/a/20033752/294884

this question is about UILabel.

Upvotes: 9

Views: 4599

Answers (2)

Kirit Modi
Kirit Modi

Reputation: 23407

Add image in your label with text as below code:

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Here is some text that is wrapping around like an image"];

    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"first.jpg"];

    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];

    [attributedString insertAttributedString:attrStringWithImage atIndex:0];

    [_lbn setAttributedText:attributedString];

Your OUTPUT :

enter image description here

Upvotes: 8

Fonix
Fonix

Reputation: 11597

You can use an NSAttributedString with an NSTextAttachment

NSTextAttachment *attachment = [[NSTextAttachment alloc]init];
[attachment setImage:<#UIImage#>];
NSAttributedString* icon = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedString insertAttributedString:icon atIndex:<#index#>];

but it will only insert the image on one line, so it wont have multiple lines down the side of it (like a newspaper article), so its only useful for small images that fit on one line (sort of like how you have it in your question i guess)

Upvotes: 2

Related Questions