Ios 用NSTextAttachment替换出现的NSString
我有一个UITextView,我想使用Ios 用NSTextAttachment替换出现的NSString,ios,objective-c,nsstring,nstextattachment,Ios,Objective C,Nsstring,Nstextattachment,我有一个UITextView,我想使用NSTextAttachment用特定图像替换一些字符串。我能够将图像附件插入到UITextView中,并且我有一种方法,可以从文本视图的属性文本中返回NSString以特定的NSString替换附件: -(NSString*)getStringFromAttributedString{ __block NSString *str = self.attributedText.string; // Trivial String representation
NSTextAttachment
用特定图像替换一些字符串。我能够将图像附件插入到UITextView
中,并且我有一种方法,可以从文本视图的属性文本中返回NSString
以特定的NSString
替换附件:
-(NSString*)getStringFromAttributedString{
__block NSString *str = self.attributedText.string; // Trivial String representation
__block NSMutableString *string = [NSMutableString new]; // To store customized text
[self.attributedText enumerateAttributesInRange:NSMakeRange(0, self.attributedText.length) options:0 usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop){
// enumerate through the attributes
NSString *v;
NSObject *x = [attributes valueForKey:@"NSAttachment"];
if(x){ // YES= This is an attachment
v = [Fuctions getObjectTag:x];
if(v == nil){
v=@"";
}
}else{
v = [str substringWithRange:range]; // NO=This is a text block.
}
// append value to string
[string appendString:v];
}];
return string;
}
现在我需要一个从NSString
返回NSAttributedString
的方法,用图像附件替换一些字符串,类似这样的方法,但我无法使它工作
-(NSMutableAttributedString*)getAttributedStringFromString:(NSString*)string{
// create NSMutableAttributedString from NSString in input
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
// create the attachment with image
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"myImageName.png"];
textAttachment.bounds = (CGRect) {0, -2, textAttachment.image.size};
// create NSMutableAttributedString with image attachment
NSMutableAttributedString *attrStringWithImage = [[NSAttributedString attributedStringWithAttachment:textAttachment] mutableCopy];
// here I'd like to replace all occurrences of a string (":D" this string for example) with my NSMutableAttributedString with image attachment).
return string;
}
我该怎么办
谢谢,希望我能解释清楚。这是我的工作解决方案:
-(NSMutableAttributedString*)getEmoticonStringFromString:(NSString*)string{
NSMutableAttributedString *final = [NSMutableAttributedString new]; //To store customized text
NSInteger len = [string length];
unichar buffer[len];
[string getCharacters:buffer range:NSMakeRange(0, len)];
for(int i = 1; i < len; i++) {
if(buffer[i-1] == ':' && buffer[i] == 'D'){
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [Functions scaleImage:[UIImage imageNamed:@"myImageName"] toSize:CGSizeMake(18.0f, 18.0f)];
textAttachment.bounds = (CGRect) {0, -2, textAttachment.image.size};
NSMutableAttributedString *attrStringWithImage = [[NSAttributedString attributedStringWithAttachment:textAttachment] mutableCopy];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init] ;
[paragraphStyle setAlignment:NSTextAlignmentLeft];
[paragraphStyle setLineSpacing:2.0f];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrString length])];
[final insertAttributedString:attrStringWithImage atIndex:final.length];
i++;
}else{
[final insertAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%c", buffer[i-1]]] atIndex:final.length];
if(i == len-1){
[final insertAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%c", buffer[i]]] atIndex:final.length];
}
}
}
return final;
-(NSMutableAttributedString*)getEmoticonStringFromString:(NSString*)字符串{
NSMUTABLeAttributeString*final=[NSMUTABLeAttributeString new];//用于存储自定义文本
NSInteger len=[字符串长度];
unichar缓冲区[len];
[字符串getCharacters:缓冲区范围:NSMakeRange(0,len)];
对于(int i=1;i
}检查
replaceCharactersRange:withAttributedString:
方法。您可以使用NSRegularExpression
或其他“查找”方法查找字符串示例中的“:D”。我不必检测编辑,因为我的文本视图不可编辑,但attributedText是动态的