gpt4 book ai didi

ios - UITextField defaultTextAttributes 错误?

转载 作者:行者123 更新时间:2023-11-29 11:25:20 24 4
gpt4 key购买 nike

所以我的问题是我的应用程序卡住了,因为它的主线程正在执行无限循环或递归。

经过一些调试我发现只有在将包含字体属性的defaultTextAttributes设置为UITextField后才会出现问题。我能够在我从头开始创建的新项目中重现这个问题,只有一个 UIViewController 从 Storyboard加载并且只包含一个 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 ]
}
}

这是暂停执行时主线程堆栈跟踪的示例:

#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

问题在模拟器 (iOS 13.2.2) 和真实设备 (iOS 13.1.3) 上均重现。Xcode 版本 11.2.1 (11B500)。是 UITextField 中的错误还是我做错了什么?

UPD:看起来我只与系统字体有关。将字体更改为例如 UIFont(name: "HelveticaNeue", size: 17) 可解决此问题。

最佳答案

我猜这是一个可报告的错误。

如果文本字段不为空,则问题似乎不会发生,因此,作为解决方法:

    let t = textField.text
textField.text = "abc"
let font = UIFont.systemFont(ofSize: 17, weight: .regular)
textField.defaultTextAttributes = [ NSAttributedString.Key.font : font ]
textField.text = t

关于ios - UITextField defaultTextAttributes 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59161109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com