如何从NSMutableAttributedString中删除文本 [英] How to remove text from NSMutableAttributedString

查看:342
本文介绍了如何从NSMutableAttributedString中删除文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从NSMutableAttributedString中删除一些文本.

I need to remove some text from NSMutableAttributedString.

我的代码如下:

[attributedLabel setText:string afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString)
    {
        NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]);
        NSRegularExpression *regexp = ParenthesisRegularExpression();
        UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:Size];
        DLog(@"%@",italicSystemFont.fontName);
        CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL);
        [regexp enumerateMatchesInString:[mutableAttributedString string] options:0 range:stringRange usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
            if (italicFont) {
                [mutableAttributedString removeAttribute:(NSString *)kCTFontAttributeName range:result.range];
                [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)italicFont range:result.range];
                CFRelease(italicFont);
            }
        }];

        return mutableAttributedString;
    }];

我想从此mutableAttributedString中删除括号.

I would like to remove the parentheses from this mutableAttributedString.

试过这个:

[[attributedLabel.attributedText stringByReplacingOccurrencesOfString:@"(" withString:@""] stringByReplacingOccurrencesOfString:@")" withString:@""];

但是有

no visible interface in NSAttributedString for stringByReplacingOccurrencesOfString.

如何从NSMutableAttributedString中删除文本?

How to I remove text from NSMutableAttributedString?

推荐答案

您为什么甚至认为NSMutableAttributedString响应消息NSMutableString响应?他们不会互相继承.相反,您应该阅读

Why do you even assume that NSMutableAttributedString responds to messages NSMutableString responds to? They don't inherit from one another. Instead you should read the documentation and use the mutableString method to get a mutable string instance to manipulate.

这篇关于如何从NSMutableAttributedString中删除文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆