want to add stickers(similar to emoji) to textview in Xcode 6? - ios

I am trying to make my emoji keyboard as default keyboard.I
want to add my images (sticker) to the textview.I am using xcode6 that allow to make our keyboard as default keyboard for all app.I can add text to the textview when click on my sticker.but not able to add that sticker to my textview.
In short,I am trying to get access of textview to add images.
Your solutions are appreciated.Thanks in advance.

You need to use NSAttributted String:
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];
source: iOS 7 TextKit - How to insert images inline with text?

Related

How can I add a custom view to an attributed string or text view?

I'm trying to get a custom view in an attributed string to be displayed on a textView. I am able to add an image with an NSTextAttachment, but it isn't what I want. I have a custom view that supports Gif's and Animated PNGs that I'd like to display between text.
Example:
text text text [customView] text [customView] text. <- In text view, preferably in attributed string
I would love some guidance as to where I should search specifically. So far I've seen related issues...
Subclass NSTextAttachment: How to subclass NSTextAttachment?
Use NSTextAttachmentContainer..?
NSTextAttachmentCell - Only OSX
Do manipulation in the text view
First, use NSAttributedString or NSMutableAttributedString to show your RichText in subviews (such as UITextView/UILabel)
Then, use NSTextAttachment to replace your image-script in text.
NSString *egText = #"hello [this_is_an_img_script]";
NSMutableAttributedString * destStr = [[NSMutableAttributedString alloc] initWithString:egText];
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
attachment.image = [UIImage imageNamed:[this_is_an_img_script]];
NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; //make your image to an attributedString
[destStr replaceCharactersInRange:range withAttributedString:textAttachmentString];
at last: [YourLabel(or YourTextView) setAttributedString:destStr];
BTW: if you use the YYkit for the RichText, you cannot use the YYAttachMentString to replace NSAttachMentString, these are different things, the UITextView(UILabel) cannot load the YYAttachMentString.
I'm looking for some way to show my gifs with the UITextView (because YYKit cannot load and preview netimage with a url, YYKit always show empty which should be a netImage, cripes!)

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)

Adding images to a UITextView

It appears that this has been asked and not answered before, but the question is ancient and there have been many Xcode / iOS updates since then, so I am going to give this a shot.
I have a simple view controller laid out. There is a single View that contains a read-only Text View with some instructions on how to use the app. I would like to intersperse some images in the scrolling text view to refer to the buttons and other elements that I am referring to in the instructions.
Here is the view:
So for instance, when the instructions refer to the green start button, I would like to insert an image of that button inline with the rest of the text.
I am using Xcode 5.1.1 and of course storyboards. Thanks in advance for any suggestions.
You can use the NSAttributedString with NSTextAttachment to attach an image to the text
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"press to start"];
NSTextAttachment *imageAttachment = [NSTextAttachment new];
imageAttachment.image = [UIImage imageNamed:#"AnyImage.png"];
NSAttributedString *stringWithImage = [NSAttributedString attributedStringWithAttachment:imageAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:stringWithImage];
self.textView.attributedText = attributedString;

Add UIImageview in Uitextview as in iMessage

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.

Resources