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

时间:2015-01-22 05:50:39

标签: ios objective-c uilabel nsattributedstring

你怎么能达到这个效果:enter image description here

也许某种NSAtributedString?

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

有什么想法吗?


注意很高兴您可以使用UITextView轻松完成此操作:

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

这个问题是关于UILabel。

2 个答案:

答案 0 :(得分: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];

OUTPUT

enter image description here

答案 1 :(得分:2)

您可以将NSAttributedStringNSTextAttachment

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

但它只会在一行上插入图像,所以它不会在它的一侧有多条线(就像一篇报纸文章),所以它只适用于适合一行的小图像(有点像你怎么样)在你的问题中我有猜测)