Search code examples
iosnsattributedstring

How to check a string contain NSattributedstring or not


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?


Solution

  • 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