使用TextView导致崩溃的归因文本

时间:2018-06-06 10:08:19

标签: ios uitextview nsattributedstring swift4.1

我有以下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
  }

此方法返回生成的属性字符串。但在这行代码崩溃

//Generate attributed string
let attributedString = getAttributedTermsAndServicesText()

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

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

  

- [__ NSCFNumber _isDefaultFace]:无法识别的选择器发送到实例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] *因未捕获的异常而终止应用   ' NSInvalidArgumentException',原因:' - [__ NSCFNumber _isDefaultFace]:   无法识别的选择器发送到实例0x15d0d9c0'   * 第一次抛出调用堆栈:(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:终止于   NSException类型的未捕获异常

但是,我仍然无法找到这次崩溃的确切原因。 有没有人以前遇到过这类问题?

1 个答案:

答案 0 :(得分:1)

请添加字体名称和大小

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
  }