Open changelee82 opened 5 years ago
Here is how I get the viewBox dynamically from the SVG @changelee82. It's probably not the most performant solution but it works for me. Thankfully the viewBox
attribute should appear very early on in the SVG source.
func viewBox(svgData: Data) -> CGRect? {
guard let string = String(data: svgData, encoding: .utf8),
let range = string.range(of: #" viewBox=\"([^"]+)\" "#, options: .regularExpression) else {
return nil
}
let matchParts = string[range].components(separatedBy: "\"")
guard matchParts.count == 3 else { return nil }
let viewBox = matchParts[1].components(separatedBy: " ").compactMap(Int.init)
guard viewBox.count == 4 else { return nil }
return .init(x: viewBox[0], y: viewBox[1], width: viewBox[2], height: viewBox[3])
}
Hello! I create a UIImageView extension, but SVG layer is not center.
I found that the file contained viewBox="0 0 100 100", So I tried to add a line of code.
`<svg version="1.1" id="p" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 100 100" enable-background="new 0 0 100 100" xml:space="preserve">
`
How to center other SVG layer? I want to get the viewBox in svg file to setting boundingBox of svgLayer.