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

标签 ios uitextview nsattributedstring swift4.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.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]: 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
  } 

关于ios - 带有导致崩溃的 TextView 的属性文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50717732/

相关文章:

ios - [CLLocationCoordinate2d]?没有名为下标的成员

ios - 在 super.init 调用之前使用了“self”,并且属性在 super.init 调用时未初始化

android - 如何停止在后台 chewie 插件中播放视频

ios - 在 iOS 中设置 textView 上字母的格式

ios - 将 HTML 字符串转换为 NSAttributedString 时运行 Archive 时崩溃

ios - 在 iOS 中获取分配的内存大小

ios - 禁用滚动后 StackView 内的 UITextView 高度为 0

iOS 从 UITextView 中删除单词

ios - 计算可以包含在具有特定大小的容器中的文本

ios - 带有表情符号的属性字符串 - 适当的高度