Ios 7 Textkit - How to Insert Images Inline With Text

iOS 7 TextKit - How to insert images inline with text?

You'll need to use an attributed string and add the image as instance of NSTextAttachment:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"like after"];

NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"whatever.png"];

NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];

[attributedString replaceCharactersInRange:NSMakeRange(4, 1) withAttributedString:attrStringWithImage];

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];
}

iOS how to insert an inline photo or video thumbnail in a UITextView?

On iOS 7 or later, you can use NSTextAttachment to insert an inline image to UITextView. See this for an example: https://stackoverflow.com/a/20930656/1262685

For the video thumbnail, you'll need to create the thumbnail yourself. Here's one way to do it: https://stackoverflow.com/a/19530997/1262685

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.

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

Newlines in iOS 7 UITextView breaking Text Kit exclusion zone wrapping

I ran into the same issue today.

This bug seem to appear, if you set editable and selectable at the same time. If only one or no is selected, it renders as expected. Both are selected by default.

Sample Image

Sample Image

If you need both options, just set them in code.

_textView.textContainer.exclusionPaths = exclusionPaths;
_textView.attributedText = attrString;
_textView.editable = YES;
_textView.selectable = YES;


Related Topics



Leave a reply



Submit