代码之家  ›  专栏  ›  技术社区  ›  blue

Swift:换行不工作?即使已设置

  •  0
  • blue  · 技术社区  · 7 年前

    好的,这只发生在我的文本的一部分,一组标签上——我多次明确地将自定义UILabel的换行样式设置为换行。然而,我只看到了文本的第二部分:

    enter image description here

    正如您所看到的,加粗的部分(即使更长)会进行换行。

    以下是自定义标签类:

    class RSSLinkLabel: UILabel {
    
        var separateSymbol = "^"
    
        var id = String()
        var bgView = UIView()
        var thinLabel = UILabel()
        var hasSetLine = Bool(false)
    
        var str = String()
    
        override init (frame : CGRect) {
            super.init(frame : frame)
    
    
        }
        func replace(myString: String, _ index: Int, _ newChar: Character) -> String {
            var modifiedString = String()
            for (i, char) in myString.characters.enumerated() {
                modifiedString += String((i == index) ? newChar : char)
            }
            return modifiedString
        }
        func customInit()
        {
            if txtSources[id] != "" && txtSources.keys.contains(id)
            {
                str = (txtSources[id]?.capitalizingFirstLetter())!
            }
    
            if (txtSources[id] != "" && !hasSetLine && txtSources.keys.contains(id))
            {
                fixText(string: str)
            }
    
    
    
            if(Network.reachability?.isReachable == false && self.text == "")
            {
               noWifiAlternative()
    
            }
    
            self.numberOfLines = 0
            self.backgroundColor = UIColor.clear
            self.clipsToBounds = false
            self.lineBreakMode = .byWordWrapping
    
        }
    
        func fixText(string: String)
        {
            var str = string
            self.textColor = barColorStr
    
            var s = NSMutableAttributedString(string: str)
            if let i = str.index(of: separateSymbol)
            {
    
                let part1 = str.substring(to: str.distance(from: str.startIndex, to: i))
                var part2 = str.substring(from: str.distance(from: str.startIndex, to: i)+1)
                part2 = part2.trimmingCharacters(in: .whitespaces)
                part2 = "\n".appending(part2)
                str = part1 + part2
    
                s = NSMutableAttributedString(string: str)
    
                s.addAttribute(NSFontAttributeName, value: overallFontThin, range: NSRange(location: str.distance(from: str.startIndex, to: i)
    
                    ,length: (str.characters.count - str.distance(from: str.startIndex, to: i))))
    
                //color
                s.addAttribute(NSForegroundColorAttributeName, value: thirdColorStr, range: NSRange(location: str.distance(from: str.startIndex, to: i)
    
                    ,length: (str.characters.count - str.distance(from: str.startIndex, to: i))))
            }
    
    
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = -1
            if let j = str.index(of: "\n")
            {
                s.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range:NSMakeRange(0, str.distance(from: str.startIndex, to: j)))
            }
            else {
                s.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range:NSMakeRange(0, s.length))
            }
    
            self.attributedText = s
            hasSetLine = true
    
            self.numberOfLines = 0
            self.clipsToBounds = false
            self.lineBreakMode = .byWordWrapping
    
            print(self.preferredMaxLayoutWidth)
        }
    
    It says my preferred max width is 0.0. Here I make the labels:
    
    for i in 0...featureLabels.count-1
            {
                featureLabels[i] = RSSLinkLabel()
                featureLabels[i]?.frame = CGRect(x: 0, y: 0, width: ((overallWidth-(imgSpace))/3).rounded(), height: heightForView(text: "n \n f \n dd \n n", font: overallFont, width: ((overallWidth-(imgSpace))/3).rounded()))
    
                featureLabels[i]?.backgroundColor = secondColorStr
                featureLabels[i]?.textAlignment = .left
    

    并从我的RSS提要内容设置:

    for i in 0...6 {
    
                if(i < xmlInfo.rssFreeList.count)
                {
    
                    var titleNew = (xmlInfo.rssFreeList[i].title.trimmingCharacters(in: .newlines)).components(separatedBy: " ")[0..<((xmlInfo.rssFreeList[i].title.trimmingCharacters(in: .newlines)).components(separatedBy: " ")).endIndex]
    
                    if(((xmlInfo.rssFreeList[i].title.trimmingCharacters(in: .newlines)).components(separatedBy: " ").count) > 4)
                    {
                        titleNew = (xmlInfo.rssFreeList[i].title.trimmingCharacters(in: .newlines)).components(separatedBy: " ")[0..<((xmlInfo.rssFreeList[i].title.trimmingCharacters(in: .newlines)).components(separatedBy: " ")).endIndex-4]
                    }
    
                    var txt = "\(titleNew.joined(separator: " "))^\(xmlInfo.rssFreeList[i].description.trimmingCharacters(in: .newlines))"
    
                    if(i > 0)
                    {
                        self.featureViews[i-1]?.downloadedFrom(link: xmlInfo.rssFreeList[i].imgStr)
                        self.featureViews[i-1]?.link = xmlInfo.rssFreeList[i].link
                        self.featureViews[i-1]?.loadCircle.isHidden = true
    
                        self.featureLabels[i-1]?.text = txt
                        self.featureLabels[i-1]?.fixText(string: txt)
    
                        featureLabels[i-1]?.lineBreakMode = .byWordWrapping
    

    正文的第二部分有什么问题?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Neha    7 年前

    将约束设置为标签,以便标签的高度随文本的增加而增加