Skip to content

Instantly share code, notes, and snippets.

@raven
Created May 2, 2018 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raven/1380a2d652ce9a4cf1af29b2c411c8e6 to your computer and use it in GitHub Desktop.
Save raven/1380a2d652ce9a4cf1af29b2c411c8e6 to your computer and use it in GitHub Desktop.
Applying `typingAttributes` whilst firstResponder on a UITextField
textField.text = "fred"
textField.becomeFirstResponder()
DispatchQueue.main.async {
// Set some typingAttributes
self.textField.typingAttributes = [NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: 18)]
// Resign first responder
self.textField.resignFirstResponder()
DispatchQueue.main.async {
self.textField.text = "bob" // 💥 Crash on iOS 10 and below
}
}
/* Stack trace
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: NSParagraphStyle)'
*** First throw call stack:
(
0 CoreFoundation 0x0000000103aeeb0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000100cea141 objc_exception_throw + 48
2 CoreFoundation 0x0000000103a09682 -[__NSDictionaryM setObject:forKey:] + 1042
3 UIKit 0x0000000101444f00 -[UILabel _setLineBreakMode:] + 219
4 UIKit 0x0000000101cc3710 -[UITextField _updateTextLabel] + 513
5 UIKit 0x0000000101cc46de -[UITextField setText:] + 221
6 defaultTextAttributes 0x000000010070fad7 _T021defaultTextAttributes14ViewControllerC11viewDidLoadyyFyycfU_yycfU_ + 279
7 defaultTextAttributes 0x0000000100710b7d _T021defaultTextAttributes14ViewControllerC11viewDidLoadyyFyycfU_yycfU_TA + 13
8 defaultTextAttributes 0x000000010070fb2d _T0Ieg_IeyB_TR + 45
9 libdispatch.dylib 0x0000000104a174a6 _dispatch_call_block_and_release + 12
10 libdispatch.dylib 0x0000000104a4005c _dispatch_client_callout + 8
11 libdispatch.dylib 0x0000000104a2140b _dispatch_main_queue_callback_4CF + 411
12 CoreFoundation 0x0000000103ab3909 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
13 CoreFoundation 0x0000000103a79ae4 __CFRunLoopRun + 2164
14 CoreFoundation 0x0000000103a79016 CFRunLoopRunSpecific + 406
15 GraphicsServices 0x00000001059faa24 GSEventRunModal + 62
16 UIKit 0x00000001011bc134 UIApplicationMain + 159
17 defaultTextAttributes 0x00000001007118f7 main + 55
18 libdyld.dylib 0x0000000104a8c65d start + 1
19 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment