@@ -250,14 +250,15 @@ public class OSRMInstructionFormatter: Formatter {
250
250
/**
251
251
Creates an instruction as an attributed string given a step and options.
252
252
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.
254
255
- parameter legIndex: Current leg index the user is currently on.
255
256
- parameter numberOfLegs: Total number of `RouteLeg` for the given `Route`.
256
257
- parameter roadClasses: Option set representing the classes of road for the `RouteStep`.
257
258
- parameter modifyValueByKey: Allows for mutating the instruction at given parts of the instruction.
258
259
- returns: An instruction as an `NSAttributedString`.
259
260
*/
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 ? {
261
262
guard let step = obj as? RouteStep else {
262
263
return nil
263
264
}
@@ -286,7 +287,7 @@ public class OSRMInstructionFormatter: Formatter {
286
287
let instructionsByModifier = instructions [ type. description] as! [ String : InstructionsByModifier ]
287
288
let defaultInstructions = instructionsByModifier [ " default " ] !
288
289
289
- wayName = NSAttributedString ( string: step. exitNames? . first ?? " " )
290
+ wayName = NSAttributedString ( string: step. exitNames? . first ?? " " , attributes : attrs )
290
291
if let _rotaryName = step. names? . first, let _ = step. exitIndex, let obj = defaultInstructions [ " name_exit " ] {
291
292
instructionObject = obj
292
293
rotaryName = _rotaryName
@@ -315,9 +316,9 @@ public class OSRMInstructionFormatter: Formatter {
315
316
let isMotorway = roadClasses? . contains ( . motorway) ?? false
316
317
317
318
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 )
321
322
wayName = phrase. replacingTokens ( using: { ( tokenType) -> NSAttributedString in
322
323
switch tokenType {
323
324
case . wayName:
@@ -329,21 +330,21 @@ public class OSRMInstructionFormatter: Formatter {
329
330
}
330
331
} )
331
332
} 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 )
333
334
if let modifyValueByKey = modifyValueByKey {
334
335
wayName = modifyValueByKey ( . code, attributedRef)
335
336
} else {
336
337
wayName = attributedRef
337
338
}
338
339
} else if name == nil , let ref = ref {
339
- let attributedRef = NSAttributedString ( string: ref)
340
+ let attributedRef = NSAttributedString ( string: ref, attributes : attrs )
340
341
if let modifyValueByKey = modifyValueByKey {
341
342
wayName = modifyValueByKey ( . code, attributedRef)
342
343
} else {
343
344
wayName = attributedRef
344
345
}
345
346
} else if let name = name {
346
- let attributedName = NSAttributedString ( string: name)
347
+ let attributedName = NSAttributedString ( string: name, attributes : attrs )
347
348
if let modifyValueByKey = modifyValueByKey {
348
349
wayName = modifyValueByKey ( . wayName, attributedName)
349
350
} else {
@@ -406,7 +407,7 @@ public class OSRMInstructionFormatter: Formatter {
406
407
if step. finalHeading != nil { bearing = Int ( step. finalHeading! as Double ) }
407
408
408
409
// Replace tokens
409
- let result = NSAttributedString ( string: instruction) . replacingTokens { ( tokenType) -> NSAttributedString in
410
+ let result = NSAttributedString ( string: instruction, attributes : attrs ) . replacingTokens { ( tokenType) -> NSAttributedString in
410
411
var replacement : String
411
412
switch tokenType {
412
413
case . code: replacement = step. codes? . first ?? " "
@@ -425,7 +426,7 @@ public class OSRMInstructionFormatter: Formatter {
425
426
if tokenType == . wayName {
426
427
return wayName // already modified above
427
428
} else {
428
- let attributedReplacement = NSAttributedString ( string: replacement)
429
+ let attributedReplacement = NSAttributedString ( string: replacement, attributes : attrs )
429
430
return modifyValueByKey ? ( tokenType, attributedReplacement) ?? attributedReplacement
430
431
}
431
432
}
0 commit comments