0

I am trying to apply line dash pattern to my UIBezierPath which is drawn on a CAShapeLayer. I had done this in web before using javascript and I got the result below for a specific set of array values:

{ var Stroke=[{'type':[{"LONG_DASH_DOT_DOT":"8,2,1,2,1,2"}]}]};

JavaScript

And using same array I draw dash pattern in Objective-C as follows:

shapeLayerForm.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:8],[NSNumber numberWithInt:2],[NSNumber numberWithInt:1],[NSNumber numberWithInt:2], [NSNumber numberWithInt:1],[NSNumber numberWithInt:2], nil];

Objective-C

Now, first screenshot is from a mac and second one is from iPad. Dimensions of both shapes the identical. As you can see, number of dashes in length of mac's rectangle is around four but in iPad's rectangle they are too many. I think this difference in length of dashes is due to resolution of the two devices, but I am not sure. Also, I want to know how to solve this issue.

blancos
  • 1,576
  • 2
  • 16
  • 38

1 Answers1

0

You could try adjusting the contentsScale property of said CAShapeLayer. You see by default that property is 1.0 leading to a 1-to-1 mapping between pixels and points, which is not what you want on a retina screen. Layers attached to UIViews get the appropriate value set by the layer, but assuming you are creating this layer yourself, you are supposed to make sure the property contains the correct value.

If you have a view at hand (for example if this code is in some UIView subclass), I suggest using shapeLayer.contentsScale = [view.layer contentsScale]; otherwise you can look at UIScreen directly.

CALayer Reference for .contentsScale
Another SO post discussing .contentsScale

Community
  • 1
  • 1
Henri Normak
  • 4,695
  • 2
  • 23
  • 33
  • Yes this code is in a UIView subclass. But how I am suppose to know the exact value of contentScale? Also, I am not getting any difference in scale even if I do shapeLayer.contentsScale = 500.0 – blancos Nov 04 '14 at 09:34
  • We'd need to see more code, technically it should be as simple as setting contents scale to 2.0 (retina) or 3.0 (for 6+), I suspect something else is going on, shapeLayerForm is a CAShapeLayer, right? And you are changing the scale on that layer? – Henri Normak Nov 04 '14 at 12:50