У меня есть эта функция, которая вставляет NSTextAttachment
в UITextView
. После того, как он вставлен, я хочу удалить определенный фрагмент текста, прежде чем он обновит textView новым NSMutableAttributedString
. Есть ли функция NSMutableAttributedString
для замены текста?
// 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
@ Anbu.Karthik Я борюсь, так как у меня нет диапазона символов, только строка ...
Вам нужно вызвать replaceCharacters(in:with:)
, который принимает NSRange и замещающую строку. Сначала вам явно нужно будет определить диапазон, но это легко.
Как определить диапазон?
см. это для справки: stackoverflow.com/questions/30468361/…