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

标签 ios swift uilabel

我不知道这里发生了什么 - 按照所有其他减少 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/

相关文章:

iOS 应用程序问题 - 如何解决?

ios - Swift:无法单击 subview 中的按钮

ios - 如何在 swift 中提取像素数据时可视化 CGImage

iphone - UILabel 并在自动调整后获取尺寸

ios - 将多个 UI 图像和 UI 标签组合成 1 个图像

iphone - 项目明智地创建供应配置文件

ios - 将 iPhone 模拟器复制到剪贴板不起作用

iphone - iPhone用户联系信息

swift - 捏合手势不适用于 SKCameraNode

iphone - 如何获得一个 UIView,它在边界更改时显示文本,使其表现得像 UILabel 一样?