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

依然范特西╮ 提交于 2019-11-27 18:07:52

问题


How could you achieve this effect:

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.


回答1:


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 :




回答2:


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)



来源:https://stackoverflow.com/questions/28082055/how-could-you-make-a-uilabel-wrap-around-an-image-like-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!