Add UIImageview in Uitextview as in iMessage - ios

I am using UItextview and adding an extra inputAccessoryview for toolbar buttons to perform additional actions.
My toolbar has a camera icon which gets the image from UIImagePickerController and Ι have to add this image in my UItextview.
Right now Ι am able to do this with :
[myTextView addSubView:imageView];
But that way, always adds the image at the beginning of the textview and what Ι want is to add it at the current cursor location. I would also like to remove this image just like I remove the text from textview.
Just for the record, Ι also tried getting UItextview text's selected range and tried inserting my imageview at that location but it did not work.
Edited Code:
NSMutableAttributedString * mas = [[NSMutableAttributedString alloc]initWithString:self.commentsTextView.text];
NSRange cursorPoistion = [self.commentsTextView selectedRange];
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSTextAttachment* onionatt = [NSTextAttachment new];
onionatt.image = chosenImage;
onionatt.bounds = CGRectMake(0,-5,200,200);
NSAttributedString* onionattchar = [NSAttributedString attributedStringWithAttachment:onionatt];
[mas insertAttributedString:onionattchar atIndex:(cursorPoistion.location + cursorPoistion.length)];
self.commentsTextView.attributedText = mas;

This was impossible before iOS 7. Now, in iOS 7, you can do it because UITextView is based on Text Kit and inline images are possible in an NSAttributedString.
Here's a very simple example where I add a picture of onions just after the word "Onions" in my text (mas is my NSMutableAttributedString):
UIImage* onions = [self thumbnailOfImageWithName:#"onion" extension:#"jpg"];
NSTextAttachment* onionatt = [NSTextAttachment new];
onionatt.image = onions;
onionatt.bounds = CGRectMake(0,-5,onions.size.width,onions.size.height);
NSAttributedString* onionattchar = [NSAttributedString attributedStringWithAttachment:onionatt];
NSRange r = [[mas string] rangeOfString:#"Onions"];
[mas insertAttributedString:onionattchar atIndex:(r.location + r.length)];
self.tv.attributedText = mas;
That's for a small inline image. It sounds like you want to add your image in a paragraph of its own, but the principle is exactly the same.

Related

Why is NSTextAttachment image not displayed?

I'm trying to add an image to an attributed string:
NSTextAttachment *locationIcon = [[NSTextAttachment alloc] init];
locationIcon.image = [UIImage imageNamed:#"location-pin-icon"];
NSAttributedString *iconString = [NSAttributedString attributedStringWithAttachment:locationIcon];
[string appendAttributedString:iconString];
NSAttributedString *locationNameString = [[NSAttributedString alloc] initWithString:#"some text" attributes:linkAttributes];
[string appendAttributedString:locationNameString];
Then I simply set myLabel.text = string; (where myLabel is a TTTAttributedLabel)
location-pin-icon is a valid image (I've checked it in debugging too). However, the location pin icon is not being displayed in the label (the following "some text" is displayed perfectly though, and linkAttributes is just a collection of system font with a custom blue color). I've also tried manually setting bounds to the text attachment, or leaving a space before the text, but nothing seems to work.
What am I doing wrong?
It was apparently due to implementation of TTTAttributedLabel. The library seems broken. When I've switched from it, the attachment started displaying correctly.

iOS Setting text background color in UILabel (with rounded rectangle shape)

I need to create something like this.
One of the way is to create like this. I can append image (for "PDF").
NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNamed:#"MyIcon.png"];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:#"My label text"]; [myString appendAttributedString:attachmentString];
myLabel.attributedText = myString;
But I not sure whether it is good. Is there any alternative way? Can I highlight 'PDF' word with rounded background? I can easily set text background color with attributed string but it won't have rounded corner.
myLabel.backgroundColor = [UIColor redColor];
myLabel.layer.cornerRedius = 5.0;
myLabel.laayer.maskToBounds = YES;
I would embed the UILabel for PDF in another UIView with the desired background color, and round the corners using view.layer.cornerRadius. This way you can adjust the UILabel how you want within the rounded view.
If you round the corners on the UILabel, you risk having your text overlap the rounded edges.
Use RTLabel library to convert the HTML text. I have used it several times. It works. Here is link to the library and a sample code.
https://github.com/honcheng/RTLabel.
and then apply the css style you need
Hope I helped.

Center both image and text inline on iOS

I have a full-width label, with dynamic text so it can be two characters or ten. I need to display an image inline on the left part, always 10px away from the first letter. Please see the example below.
For now, I just put a full-width label and at runtime, I measure the text width with boundingRectWithSize: method, and adjust my image constraints programmatically.
Do you have any good idea to build this kind of interface without measuring manually the text width?
Objective - C
You can add image as text attachment.
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
UIImage *imageTest=[UIImage imageNamed:#"arrow.png"];
attachment.image = imageTest;
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:#"My label text "];
NSMutableAttributedString *myStringWithArrow = [[NSMutableAttributedString alloc]initWithAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
[myStringWithArrow appendAttributedString:myString];
yourLabel.attributedText = myStringWithArrow;
Swift
var attachment = NSTextAttachment()
var imageTest = UIImage(named:"arrow.png")
attachment.image = imageTest
var myString = NSMutableAttributedString(string: "My label text ")
var myStringWithArrow = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
myStringWithArrow.appendAttributedString(myString)
lblAttributed.attributedText = myStringWithArrow
Output :
#ashish-kakkad's answer is perfect but unless you need a pixel-perfect image you can use a Unicode symbol:
[self.button1 setTitle:#"\u27A4 Button" forState:UIControlStateNormal];
Most of the Unicode symbols with codes could be found here http://unicode-table.com/

iOS Swift Wrapping Text Around Image

I have a UILabel that will contain various lengths of text. I need to place an image in the upper left corner of the text and have the text wrap around it. How can I do this? All I could find was using a UITextView which I don't want to use since it's static text.
This is a perfectly reasonable use of a UITextView. Your reasons for hesitation to use it are unclear. You can make the UITextView non-editable and non-selectable; the user will not know that it is a UITextView as opposed to to a UILabel.
If you don't like that solution, then what I would do is use, instead of a UILabel, a custom view that draws the text. You can draw the text with Text Kit and thus you can take complete charge of how the text draws. In particular, you can cause it to wrap however you like, including not drawing the text in the corner (exclusion path on the text container).
You can achieve this using NSTextAttachment and attributed text.
NSMutableAttributedString *myText = [[NSMutableAttributedString alloc] initWithString:labelStr];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init]
attachment.image = yourImage;
NSAttributedString *attachmentLock = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *lockString = [[NSMutableAttributedString alloc] initWithAttributedString:myText];
//set your image range within the text. modify it till you get it right.
NSRange range = NSMakeRange(0,[labelStr length]);
[lockString replaceCharactersInRange:NSMakeRange(range.location, 1) withAttributedString:attachmentLock];
yourLabel.attributedText = lockString;

NSTextAttachment not showing up in Today Notification Center Widget

I have a notification center widget that has a table view with table view cells. In the cells I have a label that I would like to show up with text + an image. The image being included as an NSTextAttachment. I have the following code inside the app:
NSTextAttachment *attachment = [NSTextAttachment new];
attachment.image = [UIImage imageWithData:item.image];
NSAttributedString *itemImage = [NSAttributedString attributedStringWithAttachment:attachment];
NSAttributedString *itemName = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#" %#", item.name]];
NSMutableAttributedString *itemString = [[NSMutableAttributedString alloc] initWithAttributedString:itemImage];
[itemString appendAttributedString:itemName];
cell.nameLabel.attributedText = itemString;
This code works while inside the app but I am also trying to use in my widget (and therefore TodayViewController). When displayed in the widget, no image shows up on the label. If I stop while running through this code I can see that the attachment.image is getting set properly. What am I doing wrong? Thanks!
It turned out later on, after I had set cell.nameLabel.attributedText = itemString; I was later setting cell.nameLabel.text = itemString, so it was overriding the attributed string
Try changing
NSTextAttachment *attachment = [NSTextAttachment new];
to
NSTextAttachment *attachment = [NSTextAttachment alloc] initWithData:nil ofType:nil];
This might be the same issue from this post: NSTextAttachment image not displayed on iOS 8 devices (with iOS7.1 sdk)

Resources