ios - 如何在 UILabel 中嵌入小图标

我需要在 iOS7 中的 UILabel 中嵌入小图标(某种自定义项目符号)。 我如何在界面设计器中做到这一点?或者至少在代码中?

在Android中,标签有leftDrawablerightDrawable,但是在iOS中是如何实现的呢? 安卓示例:

最佳答案

您可以使用 iOS 7 的 text attachments 执行此操作,它们是 TextKit 的一部分。一些示例代码:

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"MyIcon.png"];

NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];

NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@"My label text"];
[myString appendAttributedString:attachmentString];

myLabel.attributedText = myString;

https://stackoverflow.com/questions/19318421/

相关文章:

objective-c - Objective-C 中的 MD5 算法

iphone - 以编程方式选择 tableview 行

objective-c - iOS 7 sizeWithAttributes : replaceme

objective-c - 如何在 UIScrollView 内的 UIWebView 上启用放大功

iphone - 什么是使 iOS 应用程序崩溃的可靠方法?

ios - 如何根据文本长度计算 UILabel 宽度?

ios - 如何使 UIButton 文本对齐中心?使用 IB

ios - 快速执行选择器 :withObject:afterDelay: is unavailab

iphone - -ObjC 链接器标志有什么作用?

objective-c - 使用 NSLocalizedString 的最佳实践