Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 757
evenodd Avatar asked Jan 22 '15 05:01

evenodd


People also ask

What is a UILabel?

A view that displays one or more lines of informational text.

What is label swift?

Label is a user interface item in SwiftUI which enables you to display a combination of an image (icon, SF Symbol or other) and a text label in a single UI element.


2 Answers

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

like image 101
Kirit Modi Avatar answered Oct 11 '22 05:10

Kirit Modi


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)

like image 22
Fonix Avatar answered Oct 11 '22 05:10

Fonix