在UILabel之后放置一个UIImage

Seb*_*ian 0 xcode interface-builder ios

我想要一个最大宽度为100的UILabel,然后是UIImage,我该如何在界面构建器中执行此操作?我希望如果文本较短,标签小于100但UIImage就在标签后面.

小智 5

您可以使用iOS 7提供的NSTextAttachment类.不需要使用框架.

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"image.png"];
NSAttributedString *attachmentAttrString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *str= [[NSMutableAttributedString alloc] initWithString:@"hello"];
[str appendAttributedString:attachmentAttrString];
myLabel.attributedText = str;
Run Code Online (Sandbox Code Playgroud)