0

I am drawing graphs for an app which is for iPad and iPhone. I have managed to scale all the dimensions so that the graphs, axes, etc. scale correctly for different devices. But I am unsure how to scale line widths. So if I write:

context?.setLineWidth(4.0)

the line will appear quite thick on an iPhone 5 but thinner on an iPad. I would like to make it look thinner on the smaller devices.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Steblo
  • 625
  • 2
  • 12
  • 22
  • http://stackoverflow.com/questions/24059327/detect-current-device-with-ui-user-interface-idiom-in-swift ? – Larme Oct 25 '16 at 15:43
  • 1
    Try take into account the scale of the device. ie. `UIScreen.mainScreen().scale`. Returns 1.0 for non-retina devices like the iPad 2, 2.0 for retina devices like the iPhone 5, 3.0 for the 6 plus and 7 plus, etc. – Aaron Wojnowski Oct 25 '16 at 15:46
  • If I want one pixel lines, I use `1 / UIScreen.mainScreen().scale`. I have actually a subclass of `NSLayoutConstraint` that converts its `constant` this way, therefore I can just set the correct values _in pixels_ in storyboard. – Sulthan Oct 25 '16 at 16:13
  • Here's a nice extension using UIDevice for device detection: http://stackoverflow.com/questions/26028918/ios-how-to-determine-iphone-model-in-swift – arled Oct 25 '16 at 16:21

1 Answers1

1

Change the LineWidth as dynamic value based on the Graph or Screen Width, try like this

iPhone 5

let graphWidth = 320.0 //set screenWidth
var lineWidth = (graphWidth*(1.5/100)) // here you can change the percentage value
print("Line Width == > \(Int(lineWidth))") //Line Width == > 4

iPhone 6

let graphWidth = 375.0 // be dynamic graphWidth = graph width
var lineWidth = (graphWidth*(1.5/100))
print("Line Width == > \(Int(lineWidth))") //Line Width == > 5