• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 用 NSTextAttachment 替换出现的 NSString

[复制链接]
菜鸟教程小白 发表于 2022-12-13 04:10:10 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 UITextView,我想使用 NSTextAttachment 将一些字符串替换为特定图像。我能够将图像附件插入到 UITextView 并且我有一个方法,该方法从 TextView 的属性文本中返回一个 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*)getAttributedStringFromStringNSString*)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 ("" this string for example) with my NSMutableAttributedString with image attachment).

    return string;
}

我该怎么办?

谢谢,希望我自己解释一下。



Best Answer-推荐答案


这是我的工作解决方案:

-(NSMutableAttributedString*)getEmoticonStringFromStringNSString*)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;

}

关于ios - 用 NSTextAttachment 替换出现的 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27617846/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap