带有导致崩溃的 TextView 的属性文本

Attributed text with TextView causing a crash

我有以下 swift 代码用于生成属性字符串,

func getAttributedTermsAndServicesText() -> NSAttributedString {
    let text = StringConstants.RegisterConstants.termsOfUseLabelText
    let attributedString = NSMutableAttributedString(string: text)
    let termsRange = attributedString.mutableString.range(of: StringConstants.CommonConstants.termsOfUse)
    attributedString.addAttribute(NSAttributedStringKey.font, value: WebViewLinkType.termsOfUse.getLink(), range: termsRange)
    attributedString.addAttribute(NSAttributedStringKey.font, value: NSUnderlineStyle.styleSingle.rawValue, range: termsRange)

    let privacyRange = attributedString.mutableString.range(of: StringConstants.CommonConstants.privacyStatement)
    attributedString.addAttribute(NSAttributedStringKey.link, value: WebViewLinkType.privacyStatement.getLink(), range: privacyRange)
    attributedString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: privacyRange)

    print(attributedString.mutableString)
    return attributedString
  }

此方法returns 生成了属性字符串。但是在这行代码上崩溃了

//Generate attributed string
let attributedString = getAttributedTermsAndServicesText()

//crash after this line
cell.textView.attributedText = attributedString

它在控制台中打印以下详细信息,

-[__NSCFNumber _isDefaultFace]: unrecognized selector sent to instance 0x15d0d9c0 ( "4 libc++abi.dylib 0x1ca0798f + 78", "5 libc++abi.dylib 0x1ca07493 __cxa_rethrow + 90", "6 libobjc.A.dylib 0x1ca131ab objc_exception_rethrow + 42", "7 CoreFoundation 0x1d699231 CFRunLoopRunSpecific + 600", "8 CoreFoundation 0x1d698fd1 CFRunLoopRunInMode + 104" ) 2018-06-06 02:52:07.439226-0700 MyApp[370:63347] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber _isDefaultFace]: unrecognized selector sent to instance 0x15d0d9c0' * First throw call stack: (0x1d78bb3d 0x1ca13067 0x1d790fd1 0x1d78f0c3 0x1d6b7dc8 0x2292fe93 0x2292f727 0x2294bf4f 0x2294bc43 0x22a66523 0x1cc728 0x1c42c0 0x1cd0f0 0x22cebc13 0x22cebde5 0x22cd9d79 0x22cf08db 0x22a9cf0f 0x229bb483 0x20613cb7 0x20607d77 0x20607c05 0x20596839 0x205b4fdb 0x205248e3 0x1f9c31bf 0x1d9dca7f 0x1d734c19 0x1d74865f 0x1d747ebd 0x1d745eab 0x1d6991af 0x1d698fd1 0x1ee43b41 0x22a21a53 0x28d578 0x1ce864eb) libc++abi.dylib: terminating with uncaught exception of type NSException

但是,我仍然无法找到导致此崩溃的确切原因。 有人遇到过这种问题吗?

请添加字体名称和大小

func getAttributedTermsAndServicesText() -> NSAttributedString {
    let text = StringConstants.RegisterConstants.termsOfUseLabelText
    let attributedString = NSMutableAttributedString(string: text)
    let termsRange = attributedString.mutableString.range(of: StringConstants.CommonConstants.termsOfUse)

    attributedString.addAttribute(NSAttributedStringKey.link, value: WebViewLinkType.termsOfUse.getLink(), range: termsRange)
    attributedString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: termsRange)

    let privacyRange = attributedString.mutableString.range(of: StringConstants.CommonConstants.privacyStatement)
    attributedString.addAttribute(NSAttributedStringKey.link, value: WebViewLinkType.privacyStatement.getLink(), range: privacyRange)
    attributedString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: privacyRange)

    print(attributedString.mutableString)
    return attributedString
  }