gpt4 book ai didi

ios - Swift:自动换行不起作用?即使设置

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

好的,这只发生在我的文本的一部分,有一组标签 - 我多次明确地将我的自定义 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

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

最佳答案

设置标签的约束,当文本增加时标签的高度会增加

关于ios - Swift:自动换行不起作用?即使设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48965488/

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