我有一个带有图像附件的attributedString,我需要在不删除图像的情况下向其中插入文本。我要做的第一件事是检测当前附加了哪些索引的图像。

以下是我正在使用的代码,但它并非一直都有效,也许有更好的方法?

 char charTemp;
    for(int i = 0; i < [mutableAttStr.string length]; i++){
        charTemp = [mutableAttStr.string characterAtIndex:i];

        if (!isalpha(charTemp) && !isspace(charTemp) && !iscntrl(charTemp) && !ispunct(charTemp)) {
       //     NSLog(@"isAttachment");
            [attachmentArrayCharIndex addObject:[NSNumber numberWithInt:i]];
        }
    }

最佳答案

首先,使用unichar,而不是char

其次,NSTextAttachment.h声明NSAttachmentCharacter,您可以使用它确定附件在属性字符串中的位置。

10-08 07:43