Ios UIlabel()点击展开文本
因此,我正在绘制一张地图,提供华盛顿特区各点的实际数据。数据是从geojson中提取出来的,列为20个左右的点。单击该点时,会出现一个弹出窗口,显示文本的截断版本,只有10行。然而,有些文字确实很长,200行+。我想开发一种方法来扩展文本,或者有一个滚动条。基本上任何东西都可以选择打开文本。添加图像以显示其外观 您可以在此处看到图像: 这是我正在使用的代码 正在使用以下命令调用ViewController.swift中的数据Ios UIlabel()点击展开文本,ios,swift,uilabel,mapkit,Ios,Swift,Uilabel,Mapkit,因此,我正在绘制一张地图,提供华盛顿特区各点的实际数据。数据是从geojson中提取出来的,列为20个左右的点。单击该点时,会出现一个弹出窗口,显示文本的截断版本,只有10行。然而,有些文字确实很长,200行+。我想开发一种方法来扩展文本,或者有一个滚动条。基本上任何东西都可以选择打开文本。添加图像以显示其外观 您可以在此处看到图像: 这是我正在使用的代码 正在使用以下命令调用ViewController.swift中的数据 func loadInitialData() {
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)
}
}
artworkViews.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)
```
那是地图套件MKMapView吗?是的!该项目的很多内容都取自本教程,然后根据我的用例重新调整用途。