gpt4 book ai didi

ios - 将一个 UILabel 文本分隔成三个不同的 UILabel

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

我不太确定标题是否符合我的要求,但我有一个带有一堆句子的 label,我想将它们中的每一个分开到不同的 UILabel

这是我的代码

   var s: [String] = []
for (i, pred) in results.enumerated() {
let latLongArr = pred.0.components(separatedBy: "\t")
myLatitude = latLongArr[1]
myLongitude = latLongArr[2]
s.append(String(format: "%d: %@ %@ (%3.2f%%)", i + 1, myLatitude, myLongitude, pred.1 * 100))
places[i].title = String(i+1)
places[i].coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(myLatitude)!, longitude: CLLocationDegrees(myLongitude)!)
}
predictionLabel.text = s.joined(separator: "\n") // one label

UILabel 文本看起来像这样

Prediction 1: latitude longitude // first sentence
(probability%)
Prediction 2: latitude longitude // second
(probability%)
Prediction 3: latitude longitude // third
(probability%)

谢谢

编辑

我已经创建了三个标签并尝试了这段代码,不幸的是它给出了第一个结果

    self.predict1.text = s.joined(separator: "\n")
self.predict2.text = s.joined(separator: "\n")
self.predict3.text = s.joined(separator: "\n")

最佳答案

要么你设置numberOfLines在标签上为 0 和 lineBreakMode到 。 byWordWrapping或者你制作一个垂直的 statckview,为每个字符串实例化一个标签,并将标签添加到堆栈 View 的 arrangedSubviews .

编辑现在我查看了您的代码,真正的问题是行 let latLongArr = pred.0.components(separatedBy: "\t")。您的数据很可能包含尾随换行符,您需要在经度之后删除尾随换行符。

   var s: [String] = []
for (i, pred) in results.enumerated() {
let latLongArr = pred.0.components(separatedBy: "\t").replacingOccurrences(of: "\n", with: "")
myLatitude = latLongArr[1]
myLongitude = latLongArr[2]
s.append(String(format: "%d: %@ %@ (%3.2f%%)", i + 1, myLatitude, myLongitude, pred.1 * 100))
places[i].title = String(i+1)
places[i].coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(myLatitude)!, longitude: CLLocationDegrees(myLongitude)!)
}

关于ios - 将一个 UILabel 文本分隔成三个不同的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47363645/

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