-2

I have add a image to a textview's text by adding NSattributedstring.

The textview can be edited by the user.

How can I check that is the textview's text still containing the NSattributedstring?

by

UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSString *temp1 = [NSString stringWithFormat:@"%@\r\n\r\n",contenttextview.text];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:temp1 attributes:attrsDictionary];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [self imageWithImage:chosenImage scaledToSize:CGSizeMake(200, 200)];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(attributedString.length -1, 1) withAttributedString:attrStringWithImage];
contenttextview.attributedText = attributedString;

How can i check that?

Sunil Sharma
  • 2,653
  • 1
  • 25
  • 36
Waitak Jong
  • 87
  • 11
  • If you add the `NSAttributedString` you know it's range. You can check if the range still has attributes. – Desdenova Jun 16 '16 at 06:43
  • textView.attributedText length will return all the number of string in it.. – Waitak Jong Jun 16 '16 at 06:49
  • How to check has attributes? – Waitak Jong Jun 16 '16 at 06:50
  • Check its `attributedText` ? – onmyway133 Jun 16 '16 at 07:12
  • You can use a for loop from `0` to `contentView.attributedText.length-originalAttributedText.length` and use `attributedSubstringFromRange:` and compare with `isEqualToAttributedString:`. Do not forget to do the necessary checks for the ranges (contentView.attributedText.length < originalAttributedText.length`, returns FALSE, etc. If you want only to check the image: http://stackoverflow.com/questions/29152660/extract-uiimage-from-nsattributed-string/29153172#29153172 – Larme Jun 16 '16 at 08:08

1 Answers1

0

Use this code,
Got the fine code from here..

//First convert your string to array
NSArray *array = [contenttextview componentsSeparatedByString:@" "];
 for(id object in array)
 {
     //Cycle thru array and use below method that is any part of string is image? If return yes even single time that means there is a image.
     BOOL containImage = [object canBeConvertedToEncoding:NSASCIIStringEncoding];
     if(containImage)
       {
           NSLog(@"containImage: NO");
       }
       else
       {
           NSLog(@"containImage: YES");
       }
 }

//This method will be used to check if string does contain image
- (BOOL)stringContainsEmoji:(NSString *)string {
    __block BOOL returnValue = NO;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
     ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {

         const unichar hs = [substring characterAtIndex:0];
         // surrogate pair
         if (0xd800 <= hs && hs <= 0xdbff) {
             if (substring.length > 1) {
                 const unichar ls = [substring characterAtIndex:1];
                 const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                 if (0x1d000 <= uc && uc <= 0x1f77f) {
                     returnValue = YES;
                 }
             }
         } else if (substring.length > 1) {
             const unichar ls = [substring characterAtIndex:1];
             if (ls == 0x20e3) {
                 returnValue = YES;
             }

         } else {
             // non surrogate
             if (0x2100 <= hs && hs <= 0x27ff) {
                 returnValue = YES;
             } else if (0x2B05 <= hs && hs <= 0x2b07) {
                 returnValue = YES;
             } else if (0x2934 <= hs && hs <= 0x2935) {
                 returnValue = YES;
             } else if (0x3297 <= hs && hs <= 0x3299) {
                 returnValue = YES;
             } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                 returnValue = YES;
             }
         }
     }];

    return returnValue;
}

If containImage return YES that means it does have image, Remember that you must have space before and after of image.

This is for other user,<> If you want to add image in text, than use below code..

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Good   Image"];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"image.png"];
CGFloat scaleFactor = 0;
textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:scaleFactor orientation:UIImageOrientationUp];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:attrStringWithImage];
self.aTextView.attributedText = attributedString;

NSLog for Array,

array: (
    Good,
    "\Ufffc",
    Image
)

NSLog for Boolean Variable,

contianImage: NO
contianImage: YES
contianImage: NO

And this is the image i have added,
Image

For your code its working as well,
Check my log, which generated using your code,

2016-06-16 12:52:15.030 Imagetest[2394:109009] array: (
    "
\n
\Ufffc"
)
2016-06-16 12:52:15.031 Imagetest[2394:109009] contianImage: YES
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56