点击UIlabel以展开文本

问题描述

因此,我正在构建一张地图,该地图提供有关位于华盛顿特区的各个点的事实数据。数据是从geojson中提取的,列出了20个左右的点。单击该点时,会出现一个弹出窗口,显示文本的截短版本,只有10行。但是,其中一些文本确实很长,超过200行。我想开发一种扩展文本或具有可滚动条的方法。基本上任何可以选择打开文本的内容添加图像以显示其外观。

您可以在此处查看图片https://i.stack.imgur.com/yeeBa.jpg

这是我正在使用的代码

正在使用此方法在ViewController.swift中调用数据

    func loadInitialData() {
        // 1
        guard let fileName = Bundle.main.path(forResource: "PublicArt4",ofType: "json")
          else { return }
        let optionalData = try? Data(contentsOf: URL(fileURLWithPath: fileName))

        guard
          let data = optionalData,// 2
          let json = try? JSONSerialization.jsonObject(with: data),// 3
          let dictionary = json as? [String: Any],// 4
          let works = dictionary["data"] as? [[Any]]
          else { return }
        // 5
       let validWorks = works.compactMap { Artwork(json: $0) }
        artworks.append(contentsOf: validWorks)
      }

   }

articleViews.swift显示标签格式

let detailLabel = UILabel()
        detailLabel.text = artwork.locationlink
        detailLabel.text = artwork.subtitle
        detailCalloutAccessoryView = detailLabel
        detailLabel.font = UIFont(name: "Heiti TC",size: 12)
        detailLabel.numberOfLines = 10
        //detailLabel.adjustsFontSizetoFitWidth = true
        // detailLabel.minimumScaleFactor = 0.5
        // detailLabel.baselineAdjustment = .alignCenters
        // detailLabel.textAlignment  = .left

geojson的格式如下

{"data" : [ [
     1,"Washington Monument","Click here to learn more","Madison Dr NW & 15th St NW","Washington","DC",20001,"38.89013","-77.033031","www.google.com Welcome to the Washington Monument,","Mural","Some text,not sure what it is"," "
  ],...

Artwork.swift以这种方式提取数据

class Artwork: NSObject,MKAnnotation {
let title: String?
let locationName: String
let locationURL: String
let discipline: String
let coordinate: CLLocationCoordinate2D

    init(title: String,locationName: String,locationURL: String,discipline: String,coordinate: CLLocationCoordinate2D) {
  self.title = title
  self.locationName = locationName
  self.locationURL = locationURL
  self.discipline = discipline
  self.coordinate = coordinate
  
    super.init()
  }
    
      
    var subtitle: String? {
      return locationName
    }
    
    var locationlink: String? {
      return locationURL
    }
    
  init?(json: [Any]) {
    // 1
    if let title = json[2] as? String {
      self.title = title
    } else {
      self.title = "No Title"
    }
    // json[11] is the long description
    //self.locationName = json[11] as! String
    // json[12] is the short location string
    self.locationName = json[9] as! String
    self.discipline = json[10] as! String
    self.locationURL = json[3] as! String
    // 2
    if let latitude = Double(json[7] as! String),let longitude = Double(json[8] as! String) {
      self.coordinate = CLLocationCoordinate2D(latitude: latitude,longitude: longitude)
    } else {
      self.coordinate = CLLocationCoordinate2D()
    }
  }

    
  // pinTintColor for disciplines: Sculpture,Plaque,Mural,Monument,other
  var markerTintColor: UIColor  {
    switch discipline {
    case "Monument":
      return .red
    case "Mural":
      return .cyan
    case "Plaque":
      return .blue
    case "Sculpture":
      return .purple
    default:
      return .green
    }
  }

  var imageName: String? {
    if discipline == "Mural" { return "Flag" }
    return "Flag"
  }

  // Annotation right callout accessory opens this mapItem in Maps app
  func mapItem() -> MKMapItem {
    let addressDict = [CNPostalAddressstreetKey: subtitle!]
    let placemark = MKPlacemark(coordinate: coordinate,addressDictionary: addressDict)
    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = title
    return mapItem
  }
}

我尝试的一件事是将其切换到UITextView,但是我无法使其正确加载。主要是因为我不确定如何将其集成到ViewController中。

         let detailLabel = UITextView()
            detailLabel.text = artwork.locationlink
            detailLabel.text = artwork.subtitle
        detailLabel.textColor = UIColor.red
        detailLabel.selectedTextRange = detailLabel.textRange(from: detailLabel.beginningOfDocument,to: detailLabel.beginningOfDocument)
        ```

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)