ios - 将文本永久附加到 UITextView

标签 ios swift nsattributedstring

我正在尝试创建一个 UITextView,在选择时将用户句柄添加到文本的开头。

我知道可以创建另一个 UITextView 并将其放置在第二个 UITextView 的左侧,使其看起来就像它们属于同一范围的一部分,但出于格式化目的,这会容易得多如果两个部分都是同一个 TextView 的一部分...

这是我正在使用的函数及其使用示例。

func addHandle(text:String, handle:String) -> NSAttributedString {

    let convertedText = NSMutableAttributedString()

    let h = NSMutableAttributedString(string: handle)
    h.beginEditing()

    let m = NSMutableAttributedString(string: text)
    m.beginEditing()


    do {
        m.addAttributes([NSFontAttributeName: UIFont.init(name: "PingFangSC-Regular", size: 18)], range: (m.string as NSString).range(of: m.string))
        h.addAttributes([NSFontAttributeName: UIFont.init(name: "PingFangSC-Medium", size: 18)], range: (h.string as NSString).range(of: h.string))

        convertedText.append(h)
        convertedText.append(m)

        m.endEditing()
        h.endEditing()
    }
    return convertedText
}

let myHandle = "Johnny: "
let myMessage = "This is a test"

addHandle(text: myMessage, handle: myHandle)

在这种情况下,句柄和消息都可以连接起来。

然而,当文本发生变化时,如下所示,由于句柄是新文本的一部分,它会再次运行该函数并再次添加句柄。

func textViewDidBeginEditing(_ textView: UITextView) {
    let currentString: String = textView.text!
    self.t.searchBar.attributedText = addHandle(text: currentString, handle: "Johnny: ")
}

 func textViewDidChange(_ textView: UITextView) {

    let currentString: String = textView.text!
    self.t.searchBar.attributedText = addHandle(text: currentString, handle: "Johnny: ")

    UIView.animate(withDuration: 0.15, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
        self.view.layoutIfNeeded()
    }, completion: { (completed) in

    })
}

我应该如何着手修改我的函数或实现以确保

  1. 句柄总是在消息之前
  2. 用户不能从文本中删除句柄

以下应该是可能的:

约翰尼:测试消息

约翰尼:

这应该是不可能的

约翰

有什么建议吗?

编辑: 这是显示当前行为的屏幕截图。 句柄是“Johnny:”,输入的文本是“This”

enter image description here

最佳答案

我认为你应该使用类似的东西(注意我使用了 textField,但你可以为 textView 做同样的事情):

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
   var newString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
   if !newString.hasPrefix(handle) {
       newString = "\(handle): \(newString)"
   }
   textView.text = newString

   // Optional font color change for the handle
   let attributedString = NSMutableAttributedString(string: textView.text!, attributes: [ NSFontAttributeName : generalFont, NSForegroundColorAttributeName: textView.textColor ]);
   attributedString.addAttribute(NSFontAttributeName, value: anotherFont, range: NSMakeRange(0, handle.characters.count));
   textView.attributedText = attributedString
   return false
}

您还可以尝试检查 range 并拒绝编辑(返回 false),如果该范围覆盖字符串的第一个字符(您处理的位置)。

关于ios - 将文本永久附加到 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46258775/

相关文章:

ios - 如何在 iTunes 中删除失败的 iOS 构建?这可能吗?

swift - 如何将 skspritenode 添加到始终在屏幕上的随机位置

objective-c - NSTextView 封闭 ScrollView 在空格键上跳转

ios - 当 UiCollectionView 发生 scrollViewDidEndDecelerating 时如何调用单元格的函数

ios - 使用 GL_Trianglestrip OpenGL ES 2.0 绘制四边形元素

ios - 在 Swift iOS 中执行 segue 时出错

Swift:将实用程序方法创建为静态类或常规函数?

ios - 使用 UIAlertAction 通过 Swift 增加标签栏上的角标(Badge)

iphone - 丰富的 UITextView 中不显示文本属性 (iOS 6)

swift - NSAttributedString dataFromRange 生成没有任何尾部缩进的 docx 文件