斯威夫特:自封包不工作?即使设定

时间:2018-02-24 17:15:27

标签: ios swift swift3 uilabel

好吧,这只发生在我的文本的一部分,有一组标签 - 我多次明确地设置我的自定义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 Feed内容中设置:

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

这第二部分文字有什么问题?

0 个答案:

没有答案