With Swift 4 and iOS 11, you can use one of two ways to solve your problem.
# 1. Using the NSMutableAttributedString replaceCharacters(in:with:) method
NSMutableAttributedString has a replaceCharacters(in:with:) method. replaceCharacters(in:with:) has the following declaration:
Replaces characters and attributes in a given range with the characters and attributes of this attribute string.
func replaceCharacters(in range: NSRange, with attrString: NSAttributedString)
The following is a sample game code that shows how to use replaceCharacters(in:with:) to replace a substring of an NSMutableAttributedString instance NSMutableAttributedString new NSMutableAttributedString instance:
import UIKit // Set initial attributed string let initialString = "This is the initial string" let attributes = [NSAttributedStringKey.foregroundColor : UIColor.red] let mutableAttributedString = NSMutableAttributedString(string: initialString, attributes: attributes) // Set new attributed string let newString = "new" let newAttributes = [NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue] let newAttributedString = NSMutableAttributedString(string: newString, attributes: newAttributes) // Get range of text to replace guard let range = mutableAttributedString.string.range(of: "initial") else { exit(0) } let nsRange = NSRange(range, in: mutableAttributedString.string) // Replace content in range with the new content mutableAttributedString.replaceCharacters(in: nsRange, with: newAttributedString)
# 2. Using the NSMutableString replaceOccurrences(of:with:options:range:) method
NSMutableString has a replaceOccurrences(of:with:options:range:) method. replaceOccurrences(of:with:options:range:) has the following declaration:
Replaces all occurrences of a given string in a given range with another specified string, returning the number of replacements.
func replaceOccurrences(of target: String, with replacement: String, options: NSString.CompareOptions = [], range searchRange: NSRange) -> Int
The following is a sample game code that shows how to use replaceOccurrences(of:with:options:range:) to replace a substring of an NSMutableAttributedString instance NSMutableAttributedString new NSMutableAttributedString instance:
import UIKit // Set initial attributed string let initialString = "This is the initial string" let attributes = [NSAttributedStringKey.foregroundColor : UIColor.red] let mutableAttributedString = NSMutableAttributedString(string: initialString, attributes: attributes) // Set new string let newString = "new" // Replace replaceable content in mutableAttributedString with new content let totalRange = NSRange(location: 0, length: mutableAttributedString.string.count) _ = mutableAttributedString.mutableString.replaceOccurrences(of: "initial", with: newString, options: [], range: totalRange) // Get range of text that requires new attributes guard let range = mutableAttributedString.string.range(of: newString) else { exit(0) } let nsRange = NSRange(range, in: mutableAttributedString.string) // Apply new attributes to the text matching the range let newAttributes = [NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue] mutableAttributedString.setAttributes(newAttributes, range: nsRange)