I have this function which inserts a NSTextAttachment
into a UITextView
. After it is inserted I am wanting to delete a specific bit of text before it updates the textView with the new NSMutableAttributedString
. Is there a NSMutableAttributedString
function to replace text?
// Create Attachment
let attachment = NSTextAttachment()
attachment.image = image
attachment.bounds = CGRect(origin: .zero, size: image.size)
// Current Attributed String
var atStr = NSMutableAttributedString(attributedString: textView.attributedText)
// Insert Attachment
let attachmentAtStr = NSAttributedString(attachment: attachment)
if let selectedRange = textView.selectedTextRange {
let cursorIndex = textView.offset(from: textView.beginningOfDocument, to: selectedRange.start)
atStr.insert(attachmentAtStr, at: cursorIndex)
atStr.addAttributes(self.textView.typingAttributes, range: NSRange(location: cursorIndex, length: 1))
} else {
atStr.append(attachmentAtStr)
}
// Delete Specific Text (ERROR: Value of type 'NSMutableAttributedString' has no member 'replacingOccurrences')
atStr = atStr.replacingOccurrences(of: "text to replace", with: "replacement", options: [.caseInsensitive, .regularExpression])
textView.attributedText = atStr