如何在 UILabel 中嵌入小图标 [英] How to embed small icon in UILabel

查看:20
本文介绍了如何在 UILabel 中嵌入小图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to embed small icons ( sort of custom bullets ) to my UILabel in iOS7. How can I do this in interface designer? Or at least in code?

在 Android 中,标签有 leftDrawablerightDrawable,但在 iOS 中是如何完成的呢?android 中的示例:

In Android there are leftDrawable and rightDrawable for labels, but how it is done in iOS? Sample in android :

推荐答案

你可以使用 iOS 7 的 文本附件,它们是 TextKit 的一部分.一些示例代码:

You can do this with iOS 7's text attachments, which are part of TextKit. Some sample code:

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;

这篇关于如何在 UILabel 中嵌入小图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆