NSTextAttachment not showing up in Today Notification Center Widget - ios

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)

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.

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!)

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/

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

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?

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