如何在IOS中的UITextView中添加图像和文本?

我想在UITextView中添加文本和图像。应根据文本和图像的长度扩展textview。简而言之,我想要做的是,当我从相机中捕获图像或从图库中选择时,它应该显示在UITextView中,我还应该能够添加一些文本与该图像类似于Facebook。我还附加了一个图像, UITextView的外观如何。

http://img4.mukewang.com/5d847ef50001b89b05050390.jpg


一只名叫tom的猫
浏览 951回答 3
3回答

倚天杖

现在绝对可以使用+ (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment请参阅此处的 Apple文档这个例子取自另一个答案:UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,140,140)];NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"before after"];NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];textAttachment.image = [UIImage imageNamed:@"sample_image.jpg"];CGFloat oldWidth = textAttachment.image.size.width;//I'm subtracting 10px to make the image display nicely, accounting//for the padding inside the textViewCGFloat scaleFactor = oldWidth / (textView.frame.size.width - 10);textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:scaleFactor orientation:UIImageOrientationUp];NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];[attributedString replaceCharactersInRange:NSMakeRange(6, 1) withAttributedString:attrStringWithImage];textView.attributedText = attributedString;使用上面的代码将在iOS 7+上的UITextView中为您提供带有文本的图像。您可以根据需要/显示属性文本的样式,并可能设置图像的宽度以确保它适合您的textView(以及设置您自己的宽高比/比例首选项)这是一个快速测试图像:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS