soleil
soleil

Reputation: 13135

Replace character in NSMutableAttributedString

This works for a regular NSString:

NSString *newString = [myString stringByReplacingOccurrencesOfString:@"," withString:@""];

But there is no such method for NSMutableAttributedString. How could I remove all instances of a comma in an NSMutableAttributedString?

Upvotes: 3

Views: 7078

Answers (6)

Peter Lapisu
Peter Lapisu

Reputation: 21005

The compleate solution

extension NSAttributedString {
    
    func replacingOccurrences(of target: String, with replacement: String, attributes: [NSAttributedString.Key : Any]? = nil) -> NSAttributedString {
        
        let s = NSMutableAttributedString(attributedString: self)
        s.beginEditing()
        s.replaceOccurrences(of: target, with: replacement, attributes: attributes)
        s.endEditing()
        return s
        
    }
    
}

extension NSMutableAttributedString {

    func replaceOccurrences(of target: String, with replacement: String, attributes: [NSAttributedString.Key : Any]? = nil) {
        
        var searchRange = NSRange(location: 0, length: self.length)
        
        while let range = self.string.range(of: target, options: [], range: Range(searchRange, in: self.string)) {
            let nsRange = NSRange(range, in: self.string)
            self.replaceCharacters(in: nsRange, with: replacement)
            
            let newRange = NSRange(location: nsRange.location, length: replacement.count)
            if let attributes = attributes {
                self.addAttributes(attributes, range: newRange)
            }
            
            searchRange = NSRange(location: newRange.upperBound, length: self.length - newRange.upperBound)
        }
        
    }
    
}

Upvotes: 0

Augustine P A
Augustine P A

Reputation: 5068

let attrString = NSMutableAttributedString(string: "Hello <b>friend<b>")

attrString.mutableString.replaceOccurrencesOfString("<b>", withString: "", options: NSStringCompareOptions.CaseInsensitiveSearch, range: NSRange(location: 0, length: attrString.length))

Try this :)

Upvotes: 7

Darius Miliauskas
Darius Miliauskas

Reputation: 3524

The code could be applied from my answer here:

NSAttributedString *attributedString = ...;
NSAttributedString *anotherAttributedString = ...; //the string or characters which will replace

while ([attributedString.mutableString containsString:@"replace"]) {
        NSRange range = [attributedString.mutableString rangeOfString:@"replace"];
        [attributedString replaceCharactersInRange:range  withAttributedString:anotherAttributedString];
    }

Upvotes: 0

casillas
casillas

Reputation: 16813

NSString *newString= "I want to ,show, you how to achieve this";
NSMutableAttributedString *displayText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",newString]];
[[displayText mutableString] replaceOccurrencesOfString:@"," withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, displayText.string.length)];

Upvotes: 1

jalone
jalone

Reputation: 2073

You can initialize the attributed string with the stripped string with the designed init. No?

Upvotes: 0

Wain
Wain

Reputation: 119041

Do it before you create the attributed string, if you can or depending on how you source it. If you can't then you can use replaceCharactersInRange:withString: (or replaceCharactersInRange:withAttributedString:), but you need to know the range so you need to search and iterate yourself.

Upvotes: 1

Related Questions