2016-06-16 10 views
-2

Ich habe ein Bild zu einem Textview-Text hinzugefügt, indem ich NSattributedstring hinzufüge.Wie prüfe ich eine Zeichenfolge, die NSattributedstring enthält oder nicht?

Die Textansicht kann vom Benutzer bearbeitet werden.

Wie kann ich überprüfen, dass der textview-Text noch den NSattributedstring enthält?

von

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; 

Wie kann ich das überprüfen?

+0

Wenn Sie fügen die 'NSAttributedString' Sie weiß, dass es Reichweite ist. Sie können überprüfen, ob der Bereich noch Attribute enthält. – Desdenova

+0

textView.attributedText Länge wird die ganze Anzahl der Zeichenfolge in es zurückgeben .. –

+0

Wie zu prüfen, hat Attribute? –

Antwort

0

Verwenden Sie diesen Code,
Haben Sie den feinen Code aus 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; 
} 

Wenn containImage Rückkehr YES, das heißt, es Bild hat, denken Sie daran, dass Sie Raum vor und nach der Bild haben muss.

Dies ist für andere Benutzer, <> Wenn Sie Bild in Text hinzufügen möchten, als Code unten verwenden ..

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 für Array,

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

NSLog für Boolesche Variable ,

contianImage: NO 
contianImage: YES 
contianImage: NO 

Und das ist das Bild, das ich hinzugefügt haben,
Image

Für Ihren Code seine so gut funktioniert,
mein Log Überprüfen Sie, die Ihren Code generiert Verwendung

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 
+0

es kann nur die Emoji ..... –

+0

Nein, Habe mit Bild überprüft, und seine Arbeitsweise, überprüfen Sie meine Art und Weise des Hinzufügens von Bild in Text – Dilip

+0

@WaitakJong ist es funktioniert für dich? – Dilip

Verwandte Themen