简体   繁体   中英

Attributed text with TextView causing a crash

I have following swift code for generating an attributed string,

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
  }

This method returns generated attributed string. But crashes on this line of code

//Generate attributed string
let attributedString = getAttributedTermsAndServicesText()

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

It prints following details in console,

-[__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.d ylib: terminating with uncaught exception of type NSException

But still, I'm not able to get exact reason for this crash. Has anyone faced this kind of issue before?

please add font name and size

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
  } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM