3

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...

Community
  • 1
  • 1
james VB
  • 31
  • 1
  • 3
  • 1
    NSAttributedString is not a view class. It inherits directly from NSObject. Either your question is misguiding or your on the wrong path to your goal. Can you explain what it is you're trying to achieve? – d00dle Jan 09 '17 at 16:47
  • 1
    @d00dle I want to 1) determine if I can put a UIView in an attributed String like I can with UIImage. 2) if not, figure out what should I do to get my UIView in a textview inline with text. For example, one solution may be to take a "screenshot" of the uiView, and make it a uiimage, then put that image into the attributedString via text attachment. But this will not preserver animation, so I need to get this actual custom UIView in there – james VB Jan 09 '17 at 17:11

1 Answers1

0

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

tripleee
  • 175,061
  • 34
  • 275
  • 318
L.wind
  • 47
  • 6