Reputation: 1167
So my problem was that my app was freezing because its main thread was doing infinite loop or recursion.
After some debugging I found out that the problem occurs only after setting defaultTextAttributes
containing font attribute to UITextField
. I was able to reproduce the problem in new project I created from scratch with only one UIViewController
loaded from storyboard and containing only one UITextField
:
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let font = UIFont.systemFont(ofSize: 17, weight: .regular)
textField.defaultTextAttributes = [ NSAttributedString.Key.font : font ]
}
}
Here is an example of stack trace of main thread when I pause execution:
#0 0x00000001116050d0 in -[__NSSingleEntryDictionaryI isEqualToDictionary:] ()
#1 0x00000001116374b7 in CFEqual ()
#2 0x000000011802420e in TDescriptor::Equal(TDescriptor const*) const ()
#3 0x000000011802413b in TCFBase<TDescriptor>::ClassEqual(void const*, void const*) ()
#4 0x000000011cba7a94 in -[UICTFontDescriptor isEqual:] ()
#5 0x000000011cbda45a in -[_UIFontDescriptorCacheKey _isEqualToKey:] ()
#6 0x00000001117324ff in -[__NSDictionaryM objectForKey:] ()
#7 0x000000011cc1213e in __25-[_UICache objectForKey:]_block_invoke ()
#8 0x0000000112507d48 in _dispatch_client_callout ()
#9 0x00000001125169bf in _dispatch_lane_barrier_sync_invoke_and_complete ()
#10 0x000000011cc120af in -[_UICache objectForKey:] ()
#11 0x000000011cbebdce in +[UIFont _fontWithDescriptor:size:textStyleForScaling:pointSizeForScaling:maximumPointSizeAfterScaling:forIB:legibilityWeight:] ()
#12 0x000000011421570a in -[UITextField _copyFont:newSize:maxSize:] ()
#13 0x00000001142167d9 in -[UITextField _updateAutosizeStyleIfNeeded] ()
#14 0x00000001142199cd in -[UITextField _layoutContent] ()
#15 0x000000011421a0e7 in -[UITextField layoutSubviews] ()
#16 0x0000000114404d01 in -[UIView(CALayerDelegate) layoutSublayersOfLayer:] ()
#17 0x0000000115d18d41 in -[CALayer layoutSublayers] ()
#18 0x0000000115d1ef33 in CA::Layer::layout_if_needed(CA::Transaction*) ()
#19 0x0000000115d2a86a in CA::Layer::layout_and_display_if_needed(CA::Transaction*) ()
#20 0x0000000115c717c8 in CA::Context::commit_transaction(CA::Transaction*, double) ()
#21 0x0000000115ca6ad1 in CA::Transaction::commit() ()
#22 0x0000000113f37461 in __34-[UIApplication _firstCommitBlock]_block_invoke_2 ()
#23 0x000000011163504c in __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ ()
#24 0x00000001116347b8 in __CFRunLoopDoBlocks ()
#25 0x000000011162f644 in __CFRunLoopRun ()
#26 0x000000011162ee16 in CFRunLoopRunSpecific ()
#27 0x000000011a105bb0 in GSEventRunModal ()
#28 0x0000000113f1fb48 in UIApplicationMain ()
#29 0x000000010f81472b in main
Problem is reproduced both on simulator (iOS 13.2.2) and real device (iOS 13.1.3).
Xcode Version 11.2.1 (11B500).
Is it a bug in UITextField
or am I doing something wrong?
UPD: Looks like I has something to do with system fonts only. Changing the font to for example UIFont(name: "HelveticaNeue", size: 17)
fixes the issue.
Upvotes: 2
Views: 386
Reputation: 77423
I'm guessing this is a reportable bug.
The problem does not seem to occur if the text field is not blank, so, as a workaround:
let t = textField.text
textField.text = "abc"
let font = UIFont.systemFont(ofSize: 17, weight: .regular)
textField.defaultTextAttributes = [ NSAttributedString.Key.font : font ]
textField.text = t
Upvotes: 1