Skip to content

Commit 9c752fc

Browse files
committed
Added default attributes argument
Added an argument that takes a dictionary of default attributes, to match the method in Formatter.
1 parent e53bf26 commit 9c752fc

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

OSRMTextInstructions/OSRMTextInstructions.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,15 @@ public class OSRMInstructionFormatter: Formatter {
250250
/**
251251
Creates an instruction as an attributed string given a step and options.
252252

253-
- parameter step: The step to format.
253+
- parameter obj: The step to format.
254+
- parameter attrs: The default attributes to use for the returned attributed string.
254255
- parameter legIndex: Current leg index the user is currently on.
255256
- parameter numberOfLegs: Total number of `RouteLeg` for the given `Route`.
256257
- parameter roadClasses: Option set representing the classes of road for the `RouteStep`.
257258
- parameter modifyValueByKey: Allows for mutating the instruction at given parts of the instruction.
258259
- returns: An instruction as an `NSAttributedString`.
259260
*/
260-
public func attributedString(for obj: Any?, legIndex: Int?, numberOfLegs: Int?, roadClasses: RoadClasses? = RoadClasses([]), modifyValueByKey: ((TokenType, NSAttributedString) -> NSAttributedString)?) -> NSAttributedString? {
261+
public func attributedString(for obj: Any?, withDefaultAttributes attrs: [String : Any]? = nil, legIndex: Int?, numberOfLegs: Int?, roadClasses: RoadClasses? = RoadClasses([]), modifyValueByKey: ((TokenType, NSAttributedString) -> NSAttributedString)?) -> NSAttributedString? {
261262
guard let step = obj as? RouteStep else {
262263
return nil
263264
}
@@ -286,7 +287,7 @@ public class OSRMInstructionFormatter: Formatter {
286287
let instructionsByModifier = instructions[type.description] as! [String: InstructionsByModifier]
287288
let defaultInstructions = instructionsByModifier["default"]!
288289

289-
wayName = NSAttributedString(string: step.exitNames?.first ?? "")
290+
wayName = NSAttributedString(string: step.exitNames?.first ?? "", attributes: attrs)
290291
if let _rotaryName = step.names?.first, let _ = step.exitIndex, let obj = defaultInstructions["name_exit"] {
291292
instructionObject = obj
292293
rotaryName = _rotaryName
@@ -315,9 +316,9 @@ public class OSRMInstructionFormatter: Formatter {
315316
let isMotorway = roadClasses?.contains(.motorway) ?? false
316317

317318
if let name = name, let ref = ref, name != ref, !isMotorway {
318-
let attributedName = NSAttributedString(string: name)
319-
let attributedRef = NSAttributedString(string: ref)
320-
let phrase = NSAttributedString(string: self.phrase(named: .nameWithCode))
319+
let attributedName = NSAttributedString(string: name, attributes: attrs)
320+
let attributedRef = NSAttributedString(string: ref, attributes: attrs)
321+
let phrase = NSAttributedString(string: self.phrase(named: .nameWithCode), attributes: attrs)
321322
wayName = phrase.replacingTokens(using: { (tokenType) -> NSAttributedString in
322323
switch tokenType {
323324
case .wayName:
@@ -329,21 +330,21 @@ public class OSRMInstructionFormatter: Formatter {
329330
}
330331
})
331332
} else if let ref = ref, isMotorway, let decimalRange = ref.rangeOfCharacter(from: .decimalDigits), !decimalRange.isEmpty {
332-
let attributedRef = NSAttributedString(string: ref)
333+
let attributedRef = NSAttributedString(string: ref, attributes: attrs)
333334
if let modifyValueByKey = modifyValueByKey {
334335
wayName = modifyValueByKey(.code, attributedRef)
335336
} else {
336337
wayName = attributedRef
337338
}
338339
} else if name == nil, let ref = ref {
339-
let attributedRef = NSAttributedString(string: ref)
340+
let attributedRef = NSAttributedString(string: ref, attributes: attrs)
340341
if let modifyValueByKey = modifyValueByKey {
341342
wayName = modifyValueByKey(.code, attributedRef)
342343
} else {
343344
wayName = attributedRef
344345
}
345346
} else if let name = name {
346-
let attributedName = NSAttributedString(string: name)
347+
let attributedName = NSAttributedString(string: name, attributes: attrs)
347348
if let modifyValueByKey = modifyValueByKey {
348349
wayName = modifyValueByKey(.wayName, attributedName)
349350
} else {
@@ -406,7 +407,7 @@ public class OSRMInstructionFormatter: Formatter {
406407
if step.finalHeading != nil { bearing = Int(step.finalHeading! as Double) }
407408

408409
// Replace tokens
409-
let result = NSAttributedString(string: instruction).replacingTokens { (tokenType) -> NSAttributedString in
410+
let result = NSAttributedString(string: instruction, attributes: attrs).replacingTokens { (tokenType) -> NSAttributedString in
410411
var replacement: String
411412
switch tokenType {
412413
case .code: replacement = step.codes?.first ?? ""
@@ -425,7 +426,7 @@ public class OSRMInstructionFormatter: Formatter {
425426
if tokenType == .wayName {
426427
return wayName // already modified above
427428
} else {
428-
let attributedReplacement = NSAttributedString(string: replacement)
429+
let attributedReplacement = NSAttributedString(string: replacement, attributes: attrs)
429430
return modifyValueByKey?(tokenType, attributedReplacement) ?? attributedReplacement
430431
}
431432
}

0 commit comments

Comments
 (0)