How to Add Image and Text in Uitextview in Ios

How to add image and text in UITextView in IOS?

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:

Sample Image

Combining text and images in `UITextView`

If I understand your goal, and how you currently have it implemented. You should check if you need to return first.

if ((cursorPosition.x + 30) >= message.frame.width) {
message.text = message.text.stringByAppendingString("\n");
}

Then you should get the now current cursor position, and add your UIImageView there.

    let size = CGSize(width: 30, height: 30);
let img = UIImage(named: change_arr[indexPath.row]);
let addImg = UIImageView(image: UIImage(named: change_arr[indexPath.row]));
addImg.frame = CGRect(origin: newCursorPosition, size: size);
message.addSubview(addImg);

then if you need to add spaces, add them now.

As stated in the previous comments using NSTextAttachment will simplify all of this, and prevent you from having to create UIViews, etc...

it would look something like this, and you don't need to check for the cursor position, because it will handle that just like it does with text.

//create your UIImage
let image = UIImage(named: change_arr[indexPath.row]);
//create and NSTextAttachment and add your image to it.
let attachment = NSTextAttachment()
attachment.image = image
//put your NSTextAttachment into and attributedString
let attString = NSAttributedString(attachment: attachment)
//add this attributed string to the current position.
textView.textStorage.insertAttributedString(attString, atIndex: textView.selectedRange.location)

Now the image is inline, and treated like text. If the user backspaces over it, it deletes just like text.

Set Image inline with text to UITextView in Objective C

You need to just use 'UIBezierPath'

For example,

txtvHtmlString.text = @“your long text…….”;

imgView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 100, 152, 128)];

imgView.image = [UIImage imageNamed:@"smily.png"];
imgView.layer.cornerRadius = 10;
[txtvHtmlString addSubview:imgView];

Then don't forget to update bezierpath in viewDidLayoutSubviews if your text is updated.

- (void)viewDidLayoutSubviews {
    UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:imgView.frame];
    txtvHtmlString.textContainer.exclusionPaths  = @[exclusionPath];
}

UITextView with text and images combined

You can add the UIImageView as a subView of UITextView.
such like, Create an imageView with image:

UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage];
[imageView setFrame:yourFrame];
[yourTextView addSubview:imageView];

And This is source code might be helpful in your case:
https://github.com/HansPinckaers/GrowingTextView

https://github.com/enormego/EGOTextView

https://www.cocoacontrols.com/controls/imgglyph

https://www.cocoacontrols.com/controls/secoretextview

http://ios-blog.co.uk/featured-posts/ios-5-rich-text-editing-series/

how to add a background image to a UITextView

add the foll0wing two lines and check once

bioTextView.addSubview(imgView)
bioTextView.sendSubview(toBack: imgView)

or use

bioTextView.backgroundColor = UIColor(patternImage: UIImage(named: "Image.png"))

updated

let textView = UITextView(frame: CGRect(x: CGFloat(20), y: CGFloat(20), width: CGFloat(400), height: CGFloat(400 )))
textView.text = "this is a test \n this is test \n this is a test"
let img = UIImageView(frame: textView.bounds)
img.image = UIImage(named: "1.jpg")
textView.backgroundColor = UIColor.clear
textView.addSubview( img)
self.view.addSubview(textView)
textView.sendSubview(toBack: img)

you get the output

Sample Image

Image followed by Text in vertical list format using a UITextView

would it be possible to add [UIImage] into the UITextView before each word?

Yes but..., by default a UITableViewCell has an Image View that appears to the left of the default label.

https://developer.apple.com/documentation/uikit/uitableviewcell/1623270-imageview

In your tableView(_:cellForRowAt:) set the cells ImageView's image to your particular image based on the cell title:

...
//Image of milk in xcassets.images.
cell.imageView.image = UIImage(named: "milk.png")!
// or from your array of images:
cell.imageView.image =
...

Otherwise I recommend creating a custom tableViewCell programmatically or in interface builder. A google search for "custom table view cell" pulled up a number of resources.

If you still want to add it to the attributed text of a UITextView you can create a for loop to loop over the elements in your arrays and generate the attributed text, assuming you have an image array and each image has a corresponding text i.e. each array is the same length:

func generateTextFrom(_ images:[UIImage], text:[String]) -> NSMutableAttributedString {
let fullText = NSMutableAttributedString(string: "")

for i in 0.. // Create our NSTextAttachment to attach the image
let imageAttachment = NSTextAttachment()
imageAttachment.image = images[i]

// wrap the attachment in its own attributed string so we can append it
let imageString = NSAttributedString(attachment: imageAttachment)

// Then, the text
let textItem = NSAttributedString(string: text[i])

//Then append the text to image, then to fullText
fullText.append(imageString)
fullText.append(textItem)
fullText.append(NSAttributedString(string: "\n"))
}

// finally set the textview's attributed text.
return fullText
}

View Hierarchy



Related Topics



Leave a reply



Submit