gpt4 book ai didi

ios - swift 3 : setting label line height causes infinite loop?

转载 作者:行者123 更新时间:2023-11-28 12:17:16 26 4
gpt4 key购买 nike

我不知道这里发生了什么 - 按照所有其他减少 UILable 行高的答案,我有一系列标签,其文本是动态的,并且可以随时更改,因为它们是从服务器提取的文本。

因为行高似乎总是与标签的实际文本相关联,所以我遇到了应用程序可能由于无限循环而卡住的情况。我不知道在这里做什么。

这是我尝试更改行高的方法,其中 txtSources[id] 不断更新:

override init (frame : CGRect) {
super.init(frame : frame)

}
func customInit()
{
// if(bgView.frame == CGRect.zero)
// {
// bgView = UIView(frame: self.frame)
// bgView.backgroundColor = self.backgroundColor
// bgView.center = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2)
// self.addSubview(bgView)
// self.sendSubview(toBack: bgView)
// }
self.font = iphoneFont
if txtSources[id] != ""
{
str = (txtSources[id]?.capitalizingFirstLetter())!
let paragraphStyle = NSMutableParagraphStyle()

//line height size
paragraphStyle.lineSpacing = 0.1
let attrString = NSMutableAttributedString(string: str)
attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length))

self.attributedText = attrString
//self.text = str
}
if(Network.reachability?.isReachable == false && self.text == "")
{
noWifiAlternative()

}

self.numberOfLines = 0
self.backgroundColor = UIColor.clear
self.textColor = barColorStr
self.clipsToBounds = false

//self.sizeToFit()

thinLabel.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
thinLabel.textColor = barColorStr
thinLabel.textAlignment = .left
thinLabel.numberOfLines = 0
thinLabel.font = iphoneFontThin
thinLabel.center = CGPoint(x: self.bounds.width/2, y: self.bounds.height * 0.89)
self.addSubview(thinLabel)
thinLabel.text = "bkadasjdjasb"

}

func noWifiAlternative()
{
self.text = txtSourcesNoWifi[id]!
}

required public init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}

convenience init () {
self.init(frame:CGRect.zero)
}
override public func layoutSubviews() {
customInit()
}

但这不会导致无限循环:

if txtSources[id] != ""
{
str = (txtSources[id]?.capitalizingFirstLetter())!
self.text = str
}

并将 bool 标志设置为仅设置一次行高没有效果,因为在初始化后的几秒钟内,标签的文本=“”

如何降低行高?

最佳答案

请参阅我的回答的第 3 段:UIButton: when isHighlighted = true, I can only call a function by swiping my finger

您误解了在哪里/如何使用 layoutSubviews

You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.

layoutSubviews 中除了 subview 框矩形设置外没有其他代码。

对于UIViewControllers,一次性初始化的通常位置是viewDidLoad(),对于NIB加载的UIViews,awakeFromNib() ,以及用于非 NIB 加载的 UIViewinit()。 View 操作代码的通常位置是自定义私有(private)方法,例如updateUI() 检查数据,例如一个字符串,并相应地更新一些 View 。

关于ios - swift 3 : setting label line height causes infinite loop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46045301/

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