Read symbols from a mobile style

View on GitHub
Sample viewer app

Combine multiple symbols from a mobile style file into a single symbol.

Image of read symbols from mobile style 1 Image of read symbols from mobile style 2

Use case

You may choose to display individual elements of a dataset like a water infrastructure network (such as valves, nodes, or endpoints) with the same basic shape, but wish to modify characteristics of elements according to some technical specifications. Multilayer symbols lets you add or remove components or modify the colors to create advanced symbol styles.

How to use the sample

Tap "Symbol" and select symbols from each table view section to create a face emoji. A preview of the symbol is updated as selections are made. The size of the symbol can be set using the slider. Tap the map to create a point graphic using the customized emoji symbol, and tap "Clear" to clear all graphics from the display.

How it works

  1. Read a mobile style file using AGSSymbolStyle.load(completion:).
  2. Get a list of all symbols in the style by calling AGSSymbolStyle.searchSymbols(with:completion:) with the default search parameters.
  3. Add symbols to the table view sections according to their category. Display a preview of each symbol with AGSSymbol.createSwatch(withWidth:height:screen:backgroundColor:completion:).
  4. When symbol selections change, create a new multilayer symbol by passing the keys for the selected symbols into AGSSymbolStyle.symbol(forKeys:completion:). Color lock all symbol layers except the base layer and update the current symbol preview image.
  5. Create graphics symbolized with the current symbol when the user taps the map view.

Relevant API

  • AGSMultilayerPointSymbol
  • AGSMultilayerSymbol
  • AGSSymbolLayer
  • AGSSymbolStyle
  • AGSSymbolStyleSearchParameters

Offline data

A mobile style file (created using ArcGIS Pro) provides the symbols used by the sample.

About the data

The mobile style file used in this sample was created using ArcGIS Pro, and is hosted on ArcGIS Online. It contains symbol layers that can be combined to create emojis.

Additional information

While each of these symbols can be created from scratch, a more convenient workflow is to author them using ArcGIS Pro and store them in a mobile style file (.stylx). ArcGIS Runtime can read symbols from a mobile style, and you can modify and combine them as needed in your app.

Tags

advanced symbology, mobile style, multilayer, stylx

Sample Code

ReadSymbolsFromMobileStyleSymbolSettingsViewController.swiftReadSymbolsFromMobileStyleSymbolViewController.swiftReadSymbolsFromMobileStyleViewController.swift
                                                                                                                                                                                                                                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// Copyright © 2019 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import UIKit
import ArcGIS

protocol ReadSymbolsFromMobileStyleSymbolSettingsViewControllerDelegate: AnyObject {
    func symbolSettingsViewControllerSettingsDidChange(_ controller: ReadSymbolsFromMobileStyleSymbolSettingsViewController)
}

class ReadSymbolsFromMobileStyleSymbolSettingsViewController: UITableViewController {
    weak var delegate: ReadSymbolsFromMobileStyleSymbolSettingsViewControllerDelegate?

    var eyes = [AGSSymbolStyleSearchResult]() {
        didSet {
            selectedEyes = eyes.first
            tableView.reloadSections([Section.eyes.rawValue], with: .automatic)
        }
    }
    var mouths = [AGSSymbolStyleSearchResult]() {
        didSet {
            selectedMouth = mouths.first
            tableView.reloadSections([Section.mouths.rawValue], with: .automatic)
        }
    }
    var hats = [AGSSymbolStyleSearchResult]() {
        didSet {
            selectedHat = hats.first
            tableView.reloadSections([Section.hats.rawValue], with: .automatic)
        }
    }

    private(set) var selectedEyes: AGSSymbolStyleSearchResult?
    private(set) var selectedMouth: AGSSymbolStyleSearchResult?
    private(set) var selectedHat: AGSSymbolStyleSearchResult?
    private(set) var selectedColor = UIColor.yellow
    private(set) var selectedSize = 40

    enum Section: Int, CaseIterable {
        case eyes
        case mouths
        case hats
        case other
    }

    func selection(for section: Section) -> AGSSymbolStyleSearchResult? {
        switch section {
        case .eyes:
            return selectedEyes
        case .mouths:
            return selectedMouth
        case .hats:
            return selectedHat
        case .other:
            return nil
        }
    }

    func selectSearchResult(_ searchResult: AGSSymbolStyleSearchResult, in section: Section) {
        switch section {
        case .eyes:
            selectedEyes = searchResult
        case .mouths:
            selectedMouth = searchResult
        case .hats:
            selectedHat = searchResult
        case .other:
            break
        }
    }

    private var cachedImages = [IndexPath: UIImage]()
    private var imageOperations = [IndexPath: AGSCancelable]()

    func searchResultForRow(at indexPath: IndexPath) -> AGSSymbolStyleSearchResult? {
        switch Section.allCases[indexPath.section] {
        case .eyes:
            return eyes[indexPath.row]
        case .mouths:
            return mouths[indexPath.row]
        case .hats:
            return hats[indexPath.row]
        case .other:
            return nil
        }
    }

    func indexPathOfRow(for searchResult: AGSSymbolStyleSearchResult) -> IndexPath? {
        if let row = eyes.firstIndex(of: searchResult) {
            return IndexPath(row: row, section: Section.eyes.rawValue)
        } else if let row = mouths.firstIndex(of: searchResult) {
            return IndexPath(row: row, section: Section.mouths.rawValue)
        } else if let row = hats.firstIndex(of: searchResult) {
            return IndexPath(row: row, section: Section.hats.rawValue)
        } else {
            return nil
        }
    }

    @discardableResult
    func createImageForRow(at indexPath: IndexPath) -> UIImage? {
        guard let searchResult = searchResultForRow(at: indexPath) else {
            return nil
        }
        if let image = cachedImages[indexPath] {
            return image
        } else if imageOperations[indexPath] != nil {
            return nil
        } else {
            searchResult.symbol { [weak self] symbol, error in
                guard let self = self else { return }
                if let symbol = symbol {
                    let createSwatchOperation = symbol.createSwatch(withWidth: 40, height: 40, screen: .main, backgroundColor: nil) { [weak self] (image, error) in
                        guard let self = self else { return }
                        self.imageOperations[indexPath] = nil
                        if let image = image {
                            self.cachedImages[indexPath] = image
                            self.tableView.reloadRows(at: [indexPath], with: .automatic)
                        } else if let error = error {
                            self.presentAlert(title: "Error creating swatch", message: error.localizedDescription)
                        }
                    }
                    self.imageOperations[indexPath] = createSwatchOperation
                } else if let error = error {
                    self.presentAlert(error: error)
                }
            }
            return nil
        }
    }

    func cancelCreationOfImageForRow(at indexPath: IndexPath) {
        guard let operation = imageOperations.removeValue(forKey: indexPath) else { return }
        operation.cancel()
    }
}

extension ReadSymbolsFromMobileStyleSymbolSettingsViewController: UITableViewDataSourcePrefetching {
    func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
        indexPaths.forEach { createImageForRow(at: $0) }
    }

    func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
        indexPaths.forEach { cancelCreationOfImageForRow(at: $0) }
    }
}

extension ReadSymbolsFromMobileStyleSymbolSettingsViewController /* UITableViewDataSource */ {
    override func numberOfSections(in tableView: UITableView) -> Int {
        return Section.allCases.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch Section.allCases[section] {
        case .eyes:
            return eyes.count
        case .mouths:
            return mouths.count
        case .hats:
            return hats.count
        case .other:
            return 2
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let section = Section.allCases[indexPath.section]
        switch section {
        case .eyes, .mouths, .hats:
            let cell = tableView.dequeueReusableCell(withIdentifier: "basic", for: indexPath)
            cell.textLabel?.text = searchResultForRow(at: indexPath)?.name
            cell.imageView?.image = createImageForRow(at: indexPath)
            cell.accessoryType = {
                if searchResultForRow(at: indexPath) == selection(for: section) {
                    return .checkmark
                } else {
                    return .none
                }
            }()
            return cell
        case .other:
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "color", for: indexPath) as! ReadSymbolsFromMobileStyleSymbolSettingsColorCell
                cell.color = selectedColor
                return cell
            } else {
                let cell = tableView.dequeueReusableCell(withIdentifier: "size", for: indexPath) as! ReadSymbolsFromMobileStyleSymbolSettingsSizeCell
                cell.delegate = self
                cell.slider.minimumValue = Float(20)
                cell.slider.maximumValue = Float(60)
                cell.slider.value = Float(selectedSize)
                return cell
            }
        }
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        switch Section.allCases[section] {
        case .eyes:
            return "Eyes"
        case .mouths:
            return "Mouths"
        case .hats:
            return "Hats"
        case .other:
            return nil
        }
    }
}

extension ReadSymbolsFromMobileStyleSymbolSettingsViewController /* UITableViewDelegate */ {
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let section = Section.allCases[indexPath.section]
        switch section {
        case .eyes, .mouths, .hats:
            if let previousSelection = selection(for: Section.allCases[indexPath.section]),
                let indexPath = self.indexPathOfRow(for: previousSelection) {
                tableView.cellForRow(at: indexPath)?.accessoryType = .none
            }
            if let searchResult = searchResultForRow(at: indexPath) {
                selectSearchResult(searchResult, in: section)
                tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
                delegate?.symbolSettingsViewControllerSettingsDidChange(self)
            }
        case .other:
            if indexPath.row == 0 {
                let colorPickerViewController = ColorPickerViewController.instantiateWith(color: selectedColor) { [weak self] (newColor) in
                    guard let self = self else { return }
                    self.selectedColor = newColor
                    self.tableView.reloadRows(at: [indexPath], with: .none)
                    self.delegate?.symbolSettingsViewControllerSettingsDidChange(self)
                }
                show(colorPickerViewController, sender: self)
            }
        }
    }
}

extension ReadSymbolsFromMobileStyleSymbolSettingsViewController: ReadSymbolsFromMobileStyleSymbolSettingsSizeCellDelegate {
    func sizeCellSizeDidChange(_ cell: ReadSymbolsFromMobileStyleSymbolSettingsSizeCell) {
        selectedSize = Int(cell.slider.value.rounded())
        delegate?.symbolSettingsViewControllerSettingsDidChange(self)
    }
}

class ReadSymbolsFromMobileStyleSymbolSettingsColorCell: UITableViewCell {
    @IBOutlet var colorView: UIView! {
        didSet {
            colorView.layer.cornerRadius = 5
            colorView.layer.borderColor = UIColor(hue: 0, saturation: 0, brightness: 0.9, alpha: 1).cgColor
            colorView.layer.borderWidth = 1
        }
    }

    var color: UIColor? {
        didSet {
            colorView.backgroundColor = color
        }
    }
}

protocol ReadSymbolsFromMobileStyleSymbolSettingsSizeCellDelegate: AnyObject {
    func sizeCellSizeDidChange(_ cell: ReadSymbolsFromMobileStyleSymbolSettingsSizeCell)
}

class ReadSymbolsFromMobileStyleSymbolSettingsSizeCell: UITableViewCell {
    @IBOutlet var slider: UISlider!

    weak var delegate: ReadSymbolsFromMobileStyleSymbolSettingsSizeCellDelegate?

    @IBAction func updateSize() {
        delegate?.sizeCellSizeDidChange(self)
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.