0

I have a very strange requirement. Custom images needs to inserted with texts in UILabel. My first question is this possible in UILabel. I can see emojis can be fitted there because they are unicodes however custom images are pure images so how to add images and text at the same time.

STextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"myimage.png"];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:@"My label text"];
[myString appendAttributedString:attachmentString];
myLabel.attributedText = myString;

I did this however this is not happening in runtime like I want to give user option to use icon/sticker and text simultaneously.

halfer
  • 19,824
  • 17
  • 99
  • 186
Saty
  • 2,563
  • 3
  • 37
  • 88
  • I think you can do it with UITextView, [check here](http://stackoverflow.com/questions/24010035/how-to-add-image-and-text-in-uitextview-in-ios) – Tj3n Mar 27 '17 at 05:54
  • You can use NSTextAttachment in UILabel http://stackoverflow.com/a/19320626/742298 – Ashutosh Dave Mar 27 '17 at 05:58
  • @Anbu.Karthik NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNamed:@"myimage.png"]; NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@"My label text"]; [myString appendAttributedString:attachmentString]; myLabel.attributedText = myString; I did this however this is not happening in runtime like I want to give user option to use icon/sticker and text simultaneously. – Saty Mar 27 '17 at 06:37
  • when u want this `I want to give user option to use icon/sticker and text simultaneously` – Anbu.Karthik Mar 27 '17 at 06:38
  • your code is fine and correct – Anbu.Karthik Mar 27 '17 at 06:38
  • @Anbu.Karthik at the run time like user enter through keyboard "Hi Anbu" then he wants to enter your image available in pallet and – Saty Mar 27 '17 at 06:40
  • simple, create the one common method and pass the current string in that method thatsall – Anbu.Karthik Mar 27 '17 at 06:41

1 Answers1

0

Make a collection view and make it the input view for your textView or TextField. On selection of element from the collection View you can pass the image to your function as in your code snippet and it would work just fine. This is how stickers are generally added in chat based applications. Hope this helps. Please reach out in case of any problem. happy Coding.

Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
  • Thank you.. I am just doing this way only.. It was working great till images and texts are separated however issues is coming in case where user wants to send textimagetextimage and so on. What to do here... – Saty Mar 27 '17 at 10:55