我正在尝试为任何可用的 iOS 设备绘制尺子,线条/刻度之间的距离准确为 1 毫米。通常我会得到 PPI 并计算我的像素距离。使用 Objective-C,它似乎并没有以这种方式工作。
linesDist
应该在“屏幕坐标像素”中包含我的 1mm 距离。
任何想法,我怎么能做到这一点?
我的基本代码如下所示: RulerView.m,它是一个 UIView:
-(void)drawRect:(CGRect)rect
{
[[UIColor blackColor] setFill];
float linesDist = 3.0; // 1mm * ppi ??
float linesWidthShort = 15.0;
float linesWidthLong = 20.0;
for (NSInteger i = 0, count = 0; i <= self.bounds.size.height; i = i + linesDist, count++)
{
bool isLong = (int)i % 5 == 0;
float linesWidth = isLong ? linesWidthLong : linesWidthShort;
UIRectFill( (CGRect){0, i, linesWidth, 1} );
}
}
编辑 ppi 检测(真的很难看),基于以下答案:
float ppi = 0;
switch ((int)[UIScreen mainScreen].bounds.size.height) {
case 568: // iPhone 5*
case 667: // iPhone 6
ppi = 163.0;
break;
case 736: // iPhone 6+
ppi = 154.0;
break;
default:
return;
break;
}