你怎么能用uilabel包裹图像(如图所示)

eve*_*odd 9 objective-c nsattributedstring uilabel ios

你怎么能达到这个效果:在此输入图像描述

也许是某种NSAtributedString?

我曾想过只添加空格的hacky方法,但它需要根据图像的宽度动态地进行.

有任何想法吗?


请注意,您可以使用UITextView轻松完成此操作:

/sf/answers/1402362671/

这个问题是关于UILabel的.

Kir*_*odi 8

使用文本在标签中添加图片,如下所示:

    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];
Run Code Online (Sandbox Code Playgroud)

你的输出:

在此输入图像描述


Fon*_*nix 2

您可以将 anNSAttributedString与 an 一起使用NSTextAttachment

NSTextAttachment *attachment = [[NSTextAttachment alloc]init];
[attachment setImage:<#UIImage#>];
NSAttributedString* icon = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedString insertAttributedString:icon atIndex:<#index#>];
Run Code Online (Sandbox Code Playgroud)

但它只会在一行上插入图像,因此它的一侧不会有多行(就像报纸文章一样),因此它只对适合一行的小图像有用(有点像你在我猜你的问题)