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

前端之家收集整理的这篇文章主要介绍了ios – 你怎么能用uilabel包裹图像(如图所示)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你怎么能达到这个效果

也许是某种NSAtributedString?

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

有任何想法吗?

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

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

这个问题是关于UILabel的.

解决方法

使用文本在标签添加图片,如下所示:
  1. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Here is some text that is wrapping around like an image"];
  2.  
  3. NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
  4. textAttachment.image = [UIImage imageNamed:@"first.jpg"];
  5.  
  6. NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
  7.  
  8. [attributedString insertAttributedString:attrStringWithImage atIndex:0];
  9.  
  10. [_lbn setAttributedText:attributedString];

你的输出

猜你在找的iOS相关文章