Cocoanetics / DTCoreText

Methods to allow using HTML code with CoreText
BSD 2-Clause "Simplified" License
6.34k stars 1.18k forks source link

Crash calculating the bounding rect on iOS 15 #1249

Closed jaylyerly closed 3 months ago

jaylyerly commented 2 years ago

Using Xcode 13.3 and the iOS 15 SDK, when I do the following

        let maxSize = CGSize(width: width, height: .greatestFiniteMagnitude)
        let size = attributedString.boundingRect(with: maxSize,
                                     options: [.usesLineFragmentOrigin, .usesFontLeading],
                                     context: nil)

I get an unrecognized selector crash.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DTImageTextAttachment attachmentBoundsForAttributes:location:textContainer:proposedLineFragment:position:]: unrecognized selector sent to instance 0x6000031514d0'

Googling this error, I don't get a lot of hits, but it looks like maybe this was new API added in iOS 15? Is that something that needs to be added to DTCoreText to work properly with iOS 15?

odrobnik commented 2 years ago

Looks like you need to implement the new function.

jdempsey-imvu commented 2 years ago

I am running into the same unrecognized selector exception, stack dump below.

Are there plans to update the DTCoreText repository to prevent this crash?

Fatal Exception: NSInvalidArgumentException -[DTImageTextAttachment attachmentBoundsForAttributes:location:textContainer:proposedLineFragment:position:]: unrecognized selector sent to instance 0x283369b90 keyboard_arrow_up 0 CoreFoundation exceptionPreprocess 1 libobjc.A.dylib objc_exception_throw 2 CoreFoundation +[NSObject(NSObject) copyDescription] 3 CoreFoundation forwarding_ 4 CoreFoundation _CF_forwarding_prep_0 5 UIFoundation -[_NSTextAttachmentLayoutInfo _queryLayout] 6 UIFoundation __NSTextAttachmentLayoutInfoGetAscent 7 CoreText TDelegateRun::TDelegateRun(CTRun const) 8 CoreText TCFRef<CTDelegateRun> TCFBase_NEW<CTDelegateRun, CTRun>(CTRun&&) 9 CoreText TGlyphEncoder::EncodeChars(CFRange, TAttributes const&, TGlyphEncoder::Fallbacks) 10 CoreText TTypesetterAttrString::Initialize(CFAttributedString const) 11 CoreText TTypesetterAttrString::TTypesetterAttrString(__CFAttributedString const, CFDictionary const*) 12 CoreText CTLineCreateWithAttributedString 13 UIFoundation NSCoreTypesetterCreateBaseLineFromAttributedString 14 UIFoundation -[NSCoreTypesetter _stringDrawingCoreTextEngineWithOriginalString:rect:padding:graphicsContext:forceClipping:attributes:stringDrawingOptions:drawingContext:stringDrawingInterface:] 15 UIFoundation __NSStringDrawingEngine 16 UIFoundation -[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:] 17 UIKitCore -[UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:] 18 UIKitCore -[UILabel _intrinsicSizeWithinSize:] 19 UIKitCore -[UILabel _ensureBaselineMetricsReturningBounds] 20 UIKitCore -[UILabel _baselineOffsetFromBottom] 21 UIKitCore -[UILabel _invalidateBaselineConstraints] 22 UIKitCore -[UILabel _setContent:adjustingFontForAccessibilityTraits:checkingForDifferences:] 23 UIKitCore -[UILabel setAttributedText:] arrow_right 24 Scotch IMVUConversationTextCell.m - Line 70 -[IMVUConversationTextCell setMessage:] + 70

lucky781123 commented 2 years ago

I also experienced the same crash today. Are there plans to fix this problem in the new repository?

2022-04-07 10:14:58.145312+0800 Email[50893:1752109] -[DTImageTextAttachment attachmentBoundsForAttributes:location:textContainer:proposedLineFragment:position:]: unrecognized selector sent to instance 0x7b2400052950 2022-04-07 10:14:58.211304+0800 Email[50893:1752109] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DTImageTextAttachment attachmentBoundsForAttributes:location:textContainer:proposedLineFragment:position:]: unrecognized selector sent to instance 0x7b2400052950' First throw call stack: ( 0 CoreFoundation 0x00007fff203feba4 exceptionPreprocess + 242 1 libobjc.A.dylib 0x00007fff201a1be7 objc_exception_throw + 48 2 CoreFoundation 0x00007fff2040d811 +[NSObject(NSObject) instanceMethodSignatureForSelector:] + 0 3 CoreFoundation 0x00007fff204030ac __forwarding + 1433 4 CoreFoundation 0x00007fff204052d8 forwarding_prep_1_ + 120 5 UIFoundation 0x00007fff2436c570 -[_NSTextAttachmentLayoutInfo _queryLayout] + 284 6 UIFoundation 0x00007fff2436ca1a NSTextAttachmentLayoutInfoGetAscent + 25 7 CoreText 0x00007fff21093fd2 _ZN12TDelegateRunC2EPK5CTRun + 106 8 CoreText 0x00007fff20fbe195 _Z11TCFBase_NEWI13CTDelegateRunJP5CTRunEE6TCFRefIPTEDpOT0 + 93 9 CoreText 0x00007fff20fbd298 _ZN13TGlyphEncoder11EncodeCharsE7CFRangeRK11TAttributesNS_9FallbacksE + 536 10 CoreText 0x00007fff20fda3ba _ZN21TTypesetterAttrString10InitializeEPK20__CFAttributedString + 310 11 CoreText 0x00007fff20fda274 _ZN21TTypesetterAttrStringC2EPK20CFAttributedStringPK14CFDictionary + 160 12 CoreText 0x00007fff20fc2f6a CTLineCreateWithAttributedString + 85 13 UIFoundation 0x00007fff2430e877 NSCoreTypesetterCreateBaseLineFromAttributedString + 396 14 UIFoundation 0x00007fff2430a5d5 -[NSCoreTypesetter _stringDrawingCoreTextEngineWithOriginalString:rect:padding:graphicsContext:forceClipping:attributes:stringDrawingOptions:drawingContext:stringDrawingInterface:] + 2462 15 UIFoundation 0x00007fff24303833 NSStringDrawingEngine + 2015 16 UIFoundation 0x00007fff2430505c -[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:] + 595 17 Email 0x000000010a426d3b $s4Mail17EdoInfoControllerC9imageName015lottieAnimationF0010loopLottieH010customView0kL6Height8animated04htmlC009attributeC003imgM023shouldShowAppReviewLinkACSSSg_ANSbSo6UIViewCSg12CoreGraphics7CGFloatVSbSSSo18NSAttributedStringCSgATSgSbtcfc + 7995 18 Email 0x000000010a424dbd $s4Mail17EdoInfoControllerC9imageName015lottieAnimationF0010loopLottieH010customView0kL6Height8animated04htmlC009attributeC003imgM023shouldShowAppReviewLinkACSSSg_ANSbSo6UIViewCSg12CoreGraphics7CGFloatVSbSSSo18NSAttributedStringCSgATSgSbtcfC + 205 19 Email 0x00000001097e44cf $s4Mail27showConnectMoreAccountsView3navySo22UINavigationControllerCtF + 6831 20 Email 0x0000000109b7ee76 $s4Mail21EdisonAccountUIHelperC15continueAppFlowyy12CoreGraphics7CGFloatVFyycfU + 566 21 Email 0x0000000109b7eff0 $s4Mail21EdisonAccountUIHelperC15continueAppFlowyy12CoreGraphics7CGFloatVFyycfU_TA + 32 22 Email 0x0000000108affdb3 $sIeg_IeyB_TR + 83 23 Email 0x000000010896d7f2 EDOMainthread + 210 24 Email 0x0000000109b7ec01 $s4Mail21EdisonAccountUIHelperC15continueAppFlowyy12CoreGraphics7CGFloatVF + 481 25 Email 0x0000000108e67300 $s4Mail20EdisonAccountManagerC23initialCheckAndRegisteryySayAA03EdoC0CGF + 3312 26 Email 0x0000000109d1d341 $s4Mail41OnboardingCheckEmailAddressViewControllerC23connectAccountDidFinishyyF + 2129 27 Email 0x0000000109d1cac6 $s4Mail41OnboardingCheckEmailAddressViewControllerCAA25AccountConnectionDelegateA2aDP07connectH9DidFinishyyFTW + 22 28 Email 0x00000001095eed9f $s4Mail24AccountConnectionManagerC12accountAdded_5emailyAA12ProviderTypeOSStF + 1455 29 Email 0x00000001095d9196 $s4Mail24AccountConnectionManagerC20accountChangedNotifyyyypFyycfU + 17734 30 Email 0x00000001095ef0c6 $s4Mail24AccountConnectionManagerC20accountChangedNotifyyyypFyycfU_TA + 38 31 Email 0x0000000108affdb3 $sIeg_IeyB_TR + 83 32 libclang_rt.tsan_iossim_dynamic.dyl 0x0000000113acc22c _ZN6__tsanL24invoke_and_release_blockEPv + 12 33 libclang_rt.tsan_iossim_dynamic.dyl 0x0000000113acbf82 _ZN6tsanL22dispatch_callback_wrapEPv + 306 34 libdispatch.dylib 0x0000000115374a2c _dispatch_client_callout + 8 35 libdispatch.dylib 0x00000001153831f1 _dispatch_main_queue_callback_4CF + 1197 36 CoreFoundation 0x00007fff2036c84d CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 9 37 CoreFoundation 0x00007fff203670aa __CFRunLoopRun + 2772 38 CoreFoundation 0x00007fff203660f3 CFRunLoopRunSpecific + 567 39 GraphicsServices 0x00007fff2c995cd3 GSEventRunModal + 139 40 UIKitCore 0x00007fff25059f42 -[UIApplication _run] + 928 41 UIKitCore 0x00007fff2505eb5e UIApplicationMain + 101 42 Email 0x000000010a3194ad main + 93 43 dyld 0x000000011372cee9 start_sim + 10 44 ??? 0x0000000000000001 0x0 + 1 ) libc++abi: terminating with uncaught exception of type NSException dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode13.2.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_LIBRARY_PATH=/Users/liuhui/Library/Developer/Xcode/DerivedData/Mail-bdbayjtmozzztydeodemtluimmea/Build/Products/Debug-iphonesimulator:/Applications/Xcode13.2.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=@executable_path/Frameworks/libclang_rt.tsan_iossim_dynamic.dylib:/Applications/Xcode13.2.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib:/Applications/Xcode13.2.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMainThreadChecker.dylib:/Applications/Xcode13.2.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMTLCapture.dylib:/Applications/Xcode13.2.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib DYLD_FRAMEWORK_PATH=/Users/liuhui/Library/Developer/Xcode/DerivedData/Mail-bdbayjtmozzztydeodemtluimmea/Build/Products/Debug-iphonesimulator *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DTImageTextAttachment attachmentBoundsForAttributes:location:textContainer:proposedLineFragment:position:]: unrecognized selector sent to instance 0x7b2400052950' terminating with uncaught exception of type NSException CoreSimulator 783.5 - Device: iPhone 12 Pro (0CC0D2C1-680D-4461-95DD-ECFE6A8E8136) - Runtime: iOS 15.2 (19C51) - DeviceType: iPhone 12 Pro

lucky781123 commented 2 years ago

I tested it, and the same code doesn't crash on ios14

odrobnik commented 2 years ago

How about one of you guys makes a PR for it?

odrobnik commented 2 years ago

The problem is that DTCoreText is not compatible with the sizing code you are trying to use. I have a custom implementation of DTTextAttachment but I use the standard NS... prefix key in the attributes.

The way to fix that would be to replace the current implementation with NSTextAttachment subclasses which - by default - implement both the mentioned attachmentBoundsForAttributes:location:textContainer:proposedLineFragment:position: as well as the attachmentCell function.

Another possibility would be to implement those methods on DTTextAttachment.

jdempsey-imvu commented 2 years ago

With recent iOS versions, DTCoreText is no longer working well enough for HTML-rendering in production. Consequently, I am now doing my HTML rendering with UILabel and NSAttributedString with NSHTMLTextDocumentType. That HTML rendering is actually more accurate than DTCoreText. The trick is that the code needs to run in main thread, preferably asynchronously.