ios - 将图像添加到 UITextView

标签 ios image cocoa-touch uitextview

似乎这个问题之前已经被问过,但没有得到回答,但这个问题很古老,而且从那时起已经有很多 Xcode/iOS 更新,所以我打算尝试一下。

我布置了一个简单的 View Controller 。有一个 View ,其中包含只读 TextView ,其中包含有关如何使用该应用程序的一些说明。我想在滚动 TextView 中散布一些图像,以引用我在说明中提到的按钮和其他元素。

这是 View :

enter image description here

例如,当说明引用绿色开始按钮时,我想插入该按钮的图像与文本的其余部分内联。

我正在使用 Xcode 5.1.1,当然还有 Storyboard。在此先感谢您的任何建议。

最佳答案

您可以使用 NSAttributedStringNSTextAttachment 将图像附加到文本

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"press   to start"];
NSTextAttachment *imageAttachment = [NSTextAttachment new];
imageAttachment.image = [UIImage imageNamed:@"AnyImage.png"];
NSAttributedString *stringWithImage = [NSAttributedString attributedStringWithAttachment:imageAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:stringWithImage];

self.textView.attributedText = attributedString;

关于ios - 将图像添加到 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25502047/

相关文章:

ios - 应用程序如何支持 iPhone X 分辨率或屏幕尺寸?

iphone - 在 UITableView 样式中自定义 selectedBackgroundView 出站分组

ios - 如何知道是否使用 MGSwipeTableCell 删除了所有单元格

image - 您可以将Docker镜像转换/构建为完整的OS镜像吗?

java - JPEG 格式的原始数据 - JAVA

ios - 以编程方式创建一个在设备旋转时保持其大小的 UIView

iphone - 在 NSPropertyListSerialization 中处理 CFNull 对象

java - native 浏览器和 Android 应用程序之间是否可以交互?

java - ImageIO.read( new URL(imagePath)) 没有响应 HTTPS URL

iOS >> UITextView >> 自动 "Jump"到最后一行