ios - 调用 NSMutableAttributedString 的子类中的追加时应用程序崩溃

标签 ios swift nsmutableattributedstring

我有以下代码,其中我从 NSMutableAttributedString 继承了一个类,当我在我的类的方法中调用 append 方法时,应用程序崩溃了。我只是想了解原因。谁能帮帮我?

 class Str: NSMutableAttributedString {

 override init() {
    super.init()
 }

 required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
 }

 func getStr(s:String) {
    self.append(NSMutableAttributedString.init(string: s))
    print(self)
 }

}

错误信息是:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -string only defined for abstract class. Define -[string.str string]!'

最佳答案

尝试使用这段代码在属性字符串中追加字符串,希望对您有所帮助。

func getStr(_ newstr:String, attrib:[NSAttributedString.Key: Any]) {
    let newText = NSMutableAttributedString(string: newstr, attributes: attrib)
    self.append(newText)
    print(self)
 }

您应该像下面这样传递 newstr 和 attrib。

let newstr: newstr = "your text here"
let attrib: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 15)]

关于ios - 调用 NSMutableAttributedString 的子类中的追加时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53668465/

相关文章:

objective-c - 无法访问 ivar CGPDFPageRef

objective-c - 以编程方式在 iOS 中忘记/删除存储的 WiFi 网络

iOS Swift 以编程方式更改标签内的特定文本颜色

iOS:在不改变字体宽度的情况下改变 NSMutableAttributedString 的字体高度?

javascript - JavaScript Date 中的 T 分隔符是什么

ios - 什么是 _UIBaselineLayoutStrut

iphone - 设置 UIImagePickerController 的 UIBarStyle

ios - 重新加载 UIPickerView 崩溃

ios - 显示 pList 中特定的 "categories"

iOS10.3系统富文本删除线无法显示