我试图在带有标题的图像(MKSnapshot)上绘制标记,但标题没有显示。
有谁知道为什么会出现这种情况?
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "TitleTest"
let pinView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "test")
pinView.titleVisibility = MKFeatureVisibility.visible
pinView.dragState = .none
pinView.animatesWhenAdded = false
pinView.canShowCallout = false
pinView.titleVisibility = .visible
pinView.contentMode = .scaleAspectFit
pinView.bounds = CGRect(x: 0, y: 0, width: 40, height: 40)
if rect!.contains(point) {
let pinCenterOffset = pinView.centerOffset
point.x -= pinView.bounds.size.width / 2
point.y -= pinView.bounds.size.height / 2
point.x += pinCenterOffset.x
point.y += pinCenterOffset.y
}
pinView.drawHierarchy(in: CGRect(
x:point.x,
y:point.y,
width:pinView.bounds.width,
height:pinView.bounds.height),
afterScreenUpdates: true)
print("Draw a marker on iOS 11")
这是带有标记的UIImageView
的屏幕截图,但没有标题:
答案 0 :(得分:0)
标题可以在引脚位置下方的快照上绘制:
确定注释在图像坐标系中的位置snapshot.point(for: annotation.coordinate)
创建标题文本的属性,您可以使其与Apple使用的类似:
private func titleAttributes() -> [NSAttributedStringKey: NSObject] {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let titleFont = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.semibold)
let attrs = [NSAttributedStringKey.font: titleFont,
NSAttributedStringKey.paragraphStyle: paragraphStyle]
return attrs
}
在注释的位置正下方绘制:
private func drawTitle(title: String,
at point: CGPoint,
attributes: [NSAttributedStringKey: NSObject]) {
let titleSize = title.size(withAttributes: attributes)
title.draw(with: CGRect(
x: point.x - titleSize.width / 2.0,
y: point.y + 1,
width: titleSize.width,
height: titleSize.height),
options: .usesLineFragmentOrigin,
attributes: attributes,
context: nil)
}
有关代码和屏幕截图的更完整示例,请参阅my following answer(它绘制自定义图钉图像,但此处可以使用其余代码)。