1

GMSPolyline allow setting a strokeColor, but the alpha component of the colour is ignored, and neither the class nor any of its superclasses have an alpha or opacity property.

Am I missing something? Is it impossible to add non opaque overlays?

sobri
  • 1,626
  • 15
  • 28
  • 1
    Can you give sample code on how you set the alpha?. As far as I know alpha is supported. just like this code. `polyline.strokeColor=[UIColor colorWithRed:0.543603F green:0.253294F blue:0.792157F alpha:1.0F];` – goblin Jan 04 '16 at 12:10
  • 1
    `line.strokeColor = UIColor.magentaColor().colorWithAlphaComponent(0.5)`, which is the same colour code I'm using for polylines on MKMapViews. On MKMapViews it's 0.5 alpha, but on GMSPolyline it's opaque. – sobri Jan 05 '16 at 04:21

1 Answers1

0

If you have just a hexstring you should use this answer to convert you color.

I had the problem to handle an hexstring with 6 characters plus an alpha value as CGFloat.

So I changed the extension from https://stackoverflow.com/a/33397427/4420355 a little bit.

 extension UIColor {
  public convenience init?(hexString: String, alpha: CGFloat) {
    var newHexString = hexString

    if !hexString.hasPrefix("#") {
        newHexString += "#"
    }

    if newHexString.count != 7 {
        print("Invalid input - hex must have 6 characters: your hexstring \(newHexString) has \(newHexString.count) characters")
        return nil
    }

    let hex = newHexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
    var initInt32 = UInt32()
    Scanner(string: hex).scanHexInt32(&initInt32)
    let r, g, b: UInt32
    switch hex.count {
    case 6: // RGB (24-bit)
        (r, g, b) = (initInt32 >> 16, initInt32 >> 8 & 0xFF, initInt32 & 0xFF)
    default:
        (r, g, b) = (0, 0, 0)
    }
    self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: alpha)
 }
}

After this you can call it like this

gmsPolygon?.strokeColor = UIColor(hexString: "#aaaaaa", withAlpha: 0.2)
gmsPolygon?.strokeColor = UIColor(hexString: "aaaaaa", withAlpha: 0.2)
kuzdu
  • 7,124
  • 1
  • 51
  • 69