Skip to content

Commit

Permalink
[GH-1440] - Added convenience initializer to handle the composition o…
Browse files Browse the repository at this point in the history
…f text and image for road name label attachment. Refactor to retrieve the appropriate attachment for current shields on a road along the route.
  • Loading branch information
vincethecoder committed Jul 30, 2018
1 parent a5d75bf commit 795f553
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
28 changes: 27 additions & 1 deletion MapboxNavigation/InstructionPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,33 @@ class TextInstruction: ImageInstruction {}
class ShieldAttachment: ImageInstruction {}
class GenericShieldAttachment: ShieldAttachment {}
class ExitAttachment: ImageInstruction {}
class RoadNameLabelAttachment: TextInstruction {}
class RoadNameLabelAttachment: TextInstruction {
var scale: CGFloat?
var color: UIColor?

var compositeImage: UIImage? {
guard let image = image, let text = text, let color = color, let scale = scale else {
return nil
}

var currentImage: UIImage?
let textHeight = font.lineHeight
let pointY = (image.size.height - textHeight) / 2
currentImage = image.insert(text: text as NSString, color: color, font: font, atPoint: CGPoint(x: 0, y: pointY), scale: scale)

return currentImage
}

convenience init(image: UIImage, text: String, color: UIColor, font: UIFont, scale: CGFloat) {
self.init()
self.image = image
self.font = font
self.text = text
self.color = color
self.scale = scale
self.image = compositeImage ?? image
}
}

extension CGSize {
fileprivate static var greatestFiniteSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
Expand Down
39 changes: 10 additions & 29 deletions MapboxNavigation/RouteMapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -802,27 +802,27 @@ extension RouteMapViewController: NavigationViewDelegate {
if minDistanceBetweenPoints < smallestLabelDistance {
smallestLabelDistance = minDistanceBetweenPoints

if let line = feature as? MGLPolylineFeature, let name = line.attribute(forKey: "name") as? String {
currentName = name
} else if let line = feature as? MGLMultiPolylineFeature, let name = line.attribute(forKey: "name") as? String {
currentName = name
} else {
currentName = nil
}

if let line = feature as? MGLPolylineFeature {
if let name = line.attribute(forKey: "name") as? String {
currentName = name
} else if let line = feature as? MGLMultiPolylineFeature, let name = line.attribute(forKey: "name") as? String {
currentName = name
} else {
currentName = nil
}

if let text = line.attribute(forKey: "ref") as? String, let shieldRawValue = line.attribute(forKey: "shield") as? String, let reflen = line.attribute(forKey: "reflen") {

let currentShield = HighwayShield.RoadType(rawValue: shieldRawValue)
let textColor = currentShield?.textColor ?? .black

let imageName = "\(shieldRawValue)-\(reflen)"
if let image = mapView.style?.image(forName: imageName) {
currentShieldName = attributedString(withFont: UIFont.boldSystemFont(ofSize: 12.0), shieldImage: image, text: text, color: textColor)
let attachment = RoadNameLabelAttachment(image: image, text: text, color: textColor, font: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize), scale: UIScreen.main.scale)
currentShieldName = NSAttributedString(attachment: attachment)
}
}
}

}
}
}
Expand All @@ -840,25 +840,6 @@ extension RouteMapViewController: NavigationViewDelegate {
}
}

private func attributedString(withFont font: UIFont, shieldImage: UIImage, text: String, color: UIColor?) -> NSAttributedString {
let attachment = RoadNameLabelAttachment()
attachment.font = font
attachment.text = text

let textHeight = font.lineHeight
let pointY = (shieldImage.size.height - textHeight) / 2

let compositeImage = shieldImage.insert(text: (text as NSString),
color: color ?? .black,
font: font,
atPoint: CGPoint(x: 0, y: pointY),
scale: UIScreen.main.scale)

attachment.image = compositeImage

return NSAttributedString(attachment: attachment)
}

@objc func updateETA() {
guard isViewLoaded, routeController != nil else { return }
navigationView.bottomBannerView.updateETA(routeProgress: routeController.routeProgress)
Expand Down

0 comments on commit 795f553

Please sign in to comment.