MapAmapMarkerNameContentView.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // MapAmapMarkerNameContentView.swift
  3. // map_amap_ios
  4. //
  5. // Created by 诺诺诺的言 on 2025/7/22.
  6. //
  7. import Foundation
  8. class MapAnnotationMarkerTextView: UIView {
  9. // 常量定义
  10. private static let bubbleHeight: CGFloat = 24
  11. private static let triangleHeight: CGFloat = 6
  12. private static let minWidth: CGFloat = 60
  13. static var height: CGFloat {
  14. return bubbleHeight + triangleHeight
  15. }
  16. let text: String
  17. private var labelView: UILabel!
  18. private var backgroundView: UIView!
  19. private var triangleLayer: CAShapeLayer!
  20. init(text: String) {
  21. self.text = text
  22. super.init(frame: .zero)
  23. setupViews()
  24. }
  25. required init?(coder: NSCoder) {
  26. fatalError("init(coder:) has not been implemented")
  27. }
  28. private func setupViews() {
  29. createSubviews()
  30. setupConstraints()
  31. }
  32. private func createSubviews() {
  33. // 创建胶囊形状背景视图
  34. backgroundView = UIView()
  35. backgroundView.backgroundColor = .white
  36. backgroundView.layer.cornerRadius = 12 // 较大的圆角,实现胶囊形状
  37. backgroundView.layer.masksToBounds = false
  38. backgroundView.layer.shadowColor = UIColor.black.cgColor
  39. backgroundView.layer.shadowOpacity = 0.2
  40. backgroundView.layer.shadowOffset = CGSize(width: 0, height: 1)
  41. backgroundView.layer.shadowRadius = 2
  42. addSubview(backgroundView)
  43. // 创建标签视图(单行文本)
  44. labelView = UILabel()
  45. labelView.text = text
  46. labelView.font = UIFont.systemFont(ofSize: 12, weight: .regular)
  47. labelView.textColor = .black
  48. labelView.backgroundColor = .clear
  49. labelView.textAlignment = .center
  50. labelView.lineBreakMode = .byTruncatingTail
  51. labelView.numberOfLines = 1 // 限制为单行
  52. backgroundView.addSubview(labelView)
  53. // 创建气泡三角形
  54. triangleLayer = CAShapeLayer()
  55. triangleLayer.fillColor = UIColor.white.cgColor
  56. let trianglePath = UIBezierPath()
  57. trianglePath.move(to: CGPoint(x: 0, y: 0)) // 左点
  58. trianglePath.addLine(to: CGPoint(x: 5, y: 6)) // 底点
  59. trianglePath.addLine(to: CGPoint(x: 10, y: 0)) // 右点
  60. trianglePath.close()
  61. triangleLayer.path = trianglePath.cgPath
  62. layer.addSublayer(triangleLayer)
  63. }
  64. private func setupConstraints() {
  65. // 启用AutoLayout
  66. translatesAutoresizingMaskIntoConstraints = false
  67. backgroundView.translatesAutoresizingMaskIntoConstraints = false
  68. labelView.translatesAutoresizingMaskIntoConstraints = false
  69. // 背景视图约束
  70. NSLayoutConstraint.activate([
  71. backgroundView.topAnchor.constraint(equalTo: topAnchor),
  72. backgroundView.leadingAnchor.constraint(equalTo: leadingAnchor),
  73. backgroundView.trailingAnchor.constraint(equalTo: trailingAnchor),
  74. backgroundView.heightAnchor.constraint(equalToConstant: Self.bubbleHeight)
  75. ])
  76. // 标签约束
  77. NSLayoutConstraint.activate([
  78. labelView.topAnchor.constraint(equalTo: backgroundView.topAnchor),
  79. labelView.leadingAnchor.constraint(equalTo: backgroundView.leadingAnchor, constant: 8),
  80. labelView.trailingAnchor.constraint(equalTo: backgroundView.trailingAnchor, constant: -8),
  81. labelView.bottomAnchor.constraint(equalTo: backgroundView.bottomAnchor)
  82. ])
  83. // 设置整个视图的高度(包含三角形)和最小宽度
  84. NSLayoutConstraint.activate([
  85. heightAnchor.constraint(equalToConstant: Self.height),
  86. widthAnchor.constraint(greaterThanOrEqualToConstant: Self.minWidth)
  87. ])
  88. layoutIfNeeded()
  89. updateTrianglePosition()
  90. }
  91. override func layoutSubviews() {
  92. super.layoutSubviews()
  93. updateTrianglePosition()
  94. }
  95. private func updateTrianglePosition() {
  96. // 更新三角形位置到底部中心
  97. let width = bounds.width
  98. triangleLayer.frame = CGRect(x: (width - 10) / 2, y: Self.bubbleHeight, width: 10, height: Self.triangleHeight)
  99. }
  100. }