如何检查字符串是否包含NSattributedstring

时间:2016-06-16 06:38:39

标签: ios nsattributedstring

我通过添加NSattributedstring将文本添加到textview的文本中。

用户可以编辑文本视图。

如何检查textview的文本是否还包含NSattributedstring?

通过

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;

我该怎么检查?

1 个答案:

答案 0 :(得分:0)

使用此代码,
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;
}

如果containImage返回YES表示它确实有图像,请记住,图像前后必须有空格。

这适用于其他用户,&lt;&gt; 如果你想在文本中添加图像,而不是使用下面的代码..

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

contianImage: NO
contianImage: YES
contianImage: NO

这是我添加的图像,
Image

对于你的代码,它的工作原理,
检查使用您的代码生成的日志,

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