似乎这个问题之前已经被问过,但没有得到回答,但这个问题很古老,而且从那时起已经有很多 Xcode/iOS 更新,所以我打算尝试一下。
我布置了一个简单的 View Controller 。有一个 View ,其中包含只读 TextView ,其中包含有关如何使用该应用程序的一些说明。我想在滚动 TextView 中散布一些图像,以引用我在说明中提到的按钮和其他元素。
这是 View :
例如,当说明引用绿色开始按钮时,我想插入该按钮的图像与文本的其余部分内联。
我正在使用 Xcode 5.1.1,当然还有 Storyboard。在此先感谢您的任何建议。
最佳答案
您可以使用 NSAttributedString
和 NSTextAttachment
将图像附加到文本
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/