I want to add both text and image in UITextView. The textview should be expanded according to the length of the text and image. In short what I want to do is that when I capture an image from camera or pick from gallery then it should display in UITextView and I should also be able to add some text with that image similar to Facebook.I am also attaching an image that how the UITextView will look like.
Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”. After that, drag and drop an image into the newly create Image Set, placing it at appropriate 1x, 2x or 3x slot.
This is absolutely possible now, using
+ (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment
See Apple docs here
And this example taken from this other answer:
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 textView CGFloat 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;
Using the above code will get you an image with text inside a UITextView on iOS 7+. You can/show style the attributed text as you want it and probably set the width of the image to make sure it fits within your textView (as well as setting your own aspect ratio/scale preference)
Here's a quick test image:
Thank you for your code, it actually worked. I make a code in the Swift, so I would like to share Swift version of your code. I checked this code works too.
let textView = UITextView(frame: CGRectMake(50, 50, 200, 300)) let attributedString = NSMutableAttributedString(string: "before after") let textAttachment = NSTextAttachment() textAttachment.image = UIImage(named: "sample_image.jpg")! let oldWidth = textAttachment.image!.size.width; //I'm subtracting 10px to make the image display nicely, accounting //for the padding inside the textView let scaleFactor = oldWidth / (textView.frame.size.width - 10); textAttachment.image = UIImage(CGImage: textAttachment.image!.CGImage, scale: scaleFactor, orientation: .Up) var attrStringWithImage = NSAttributedString(attachment: textAttachment) attributedString.replaceCharactersInRange(NSMakeRange(6, 1), withAttributedString: attrStringWithImage) textView.attributedText = attributedString; self.view.addSubview(textView)
Code For Swift 3.0
var attributedString :NSMutableAttributedString! attributedString = NSMutableAttributedString(attributedString:txtBody.attributedText) let textAttachment = NSTextAttachment() textAttachment.image = image let oldWidth = textAttachment.image!.size.width; //I'm subtracting 10px to make the image display nicely, accounting //for the padding inside the textView let scaleFactor = oldWidth / (txtBody.frame.size.width - 10); textAttachment.image = UIImage(cgImage: textAttachment.image!.cgImage!, scale: scaleFactor, orientation: .up) let attrStringWithImage = NSAttributedString(attachment: textAttachment) attributedString.append(attrStringWithImage) txtBody.attributedText = attributedString;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With