gpt4 book ai didi

ios - 将 UILabel.attributedText 设置为相同的值很昂贵吗?

转载 作者:搜寻专家 更新时间:2023-11-01 06:03:34 24 4
gpt4 key购买 nike

我的应用程序中有一个 View Controller ,用于调试以显示应用程序的一些内部结构(仅适用于本地 xcode 构建,应用程序商店版本没有此 Controller )。

在这个 Controller 中,我有一个标签,我想反射(reflect)内部组件的状态(具体来说,我希望它显示该组件是启用还是禁用)。

我的问题:

#1:将 UILabel 的 .attributedText 属性设置为与之前相同的值是否很昂贵,或者我应该缓存旧值并仅在以下情况下设置属性它改变了吗?

#2:.text(非属性)属性怎么样?

我目前正在使用以下代码:

// Schedule timer to update the control panel. (This is debug-only, so not worth
// the complexity of making this event-based)
Timer.scheduledTimer(withTimeInterval: 0.5,
repeats: true) { [weak self] timer in

DispatchQueue.main.async {

// Stop timer if we've been dealloced or are no longer being presented
guard let strongSelf = self,
strongSelf.isBeingPresented else
{
timer.invalidate()
return
}

// "Something Enabled / Disabled" label
let somethingIsEnabled = strongSelf.someDepenency.isEnabled
let somethingEnabledString = NSMutableAttributedString(string: "Something ")

somethingEnabledString.append(NSAttributedString(string: isEnabled ? "Enabled" : "Disabled",
attributes: isEnabled ? nil : [NSForegroundColorAttributeName: UIColor(xtHardcodedHexValue: "0xCD0408")]))
strongSelf.somethingEnabledLabel?.attributedText = somethingEnabledString

}
}

最佳答案

在分享一些数字之前,我强烈建议不要执行此类过早的优化。考虑以下代码:

private func getAttributedString() -> NSMutableAttributedString{
let attributedString = NSMutableAttributedString(string: "Something ")
attributedString.append(NSAttributedString(string: "Enabled",
attributes: [NSForegroundColorAttributeName: UIColor(rgb: 0xCD0408)]))
return attributedString
}

//overwrites attributed text 100000 times
@IBAction func overwriteAttributedText(_ sender: Any) {
let timeBeforeAction = Date.init()
print ("Time taken to overwrite attributed text is ")
for _ in 1 ... 100000{
label.attributedText = getAttributedString()
}
let timeAfterAction = Date.init()
let timeTaken = timeAfterAction.timeIntervalSince(timeBeforeAction)
print(timeTaken)
}

//overwrites attributed text 100 times
@IBAction func cacheAttributedText(_ sender: Any) {
let timeBeforeAction = Date.init()
print ("Time taken to selectively overwrite attributed text is ")
for i in 1 ... 100000{
if i % 1000 == 0 {
label.attributedText = getAttributedString()
}
}
let timeAfterAction = Date.init()
let timeTaken = timeAfterAction.timeIntervalSince(timeBeforeAction)
print(timeTaken)
}

//overwrites text 100000 times
@IBAction func overWriteText(_ sender: Any) {
let defaultText = "Hello World"
let timeBeforeAction = Date.init()
print ("Time taken to overwrite text is ")
for _ in 1 ... 100000{
label.text = defaultText
}
let timeAfterAction = Date.init()
let timeTaken = timeAfterAction.timeIntervalSince(timeBeforeAction)
print(timeTaken)
}

结果如下:

Time taken to overwrite attributed text is 0.597925961017609
Time taken to selectively overwrite attributed text is 0.004891037940979
Time taken to overwrite text is 0.0462920069694519

结果不言而喻,但如果需要此类优化,我会留给您。

关于ios - 将 UILabel.attributedText 设置为相同的值很昂贵吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43592287/

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