将一个UILabel文本分离到三个不同的UILabel



我不太确定标题是否符合我想要的,但我有一个包含一堆句子的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,为每个字符串实例化一个标签,并将标签添加到堆栈视图的排列子视图中。

编辑现在我看了你的代码,真正的问题是行 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)!)
}

最新更新