iOS Swift环绕图像的文本

时间:2015-04-06 14:19:58

标签: ios swift uilabel

我有一个包含各种长度文本的UILabel。我需要在文本的左上角放置一个图像并让文本环绕它。我怎样才能做到这一点?我只能找到一个我不想使用的UITextView,因为它是静态文本。

2 个答案:

答案 0 :(得分:4)

这是UITextView的完全合理使用。您犹豫使用它的原因尚不清楚。您可以使UITextView不可编辑且不可选;用户不会知道它是UITextView而不是UILabel。

如果您不喜欢该解决方案,那么我要做的是使用绘制文本的自定义视图而不是UILabel。您可以使用文本工具包绘制文本,因此您可以完全了解文本的绘制方式。特别是,您可以根据需要对其进行换行,包括不在角落中绘制文本(文本容器上的排除路径)。

答案 1 :(得分:0)

您可以使用NSTextAttachment和属性文本来实现此目的。

NSMutableAttributedString *myText = [[NSMutableAttributedString alloc] initWithString:labelStr];

NSTextAttachment *attachment = [[NSTextAttachment alloc] init]
attachment.image = yourImage;
NSAttributedString *attachmentLock = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *lockString = [[NSMutableAttributedString alloc] initWithAttributedString:myText];

//set your image range within the text. modify it till you get it right.
NSRange range = NSMakeRange(0,[labelStr length]);
[lockString replaceCharactersInRange:NSMakeRange(range.location, 1) withAttributedString:attachmentLock];
yourLabel.attributedText = lockString;
相关问题