Skip to content

Commit

Permalink
More accurate stepWidth calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
spromicky committed Nov 19, 2017
1 parent 922e6e4 commit ccef742
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions StepSlider/source/StepSlider/StepSlider.m
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ - (void)layoutLayersAnimated:(BOOL)animated
CATextLayer *trackLabel;

if (self.labels.count) {
trackLabel = [self textLayerWithSize:CGSizeMake(stepWidth, labelsHeight - self.labelOffset) index:i];
trackLabel = [self textLayerWithSize:CGSizeMake([self roundForTextDrawing:stepWidth], labelsHeight - self.labelOffset) index:i];
}

if (i < _trackCirclesArray.count) {
Expand Down Expand Up @@ -308,16 +308,16 @@ - (CGFloat)labelHeightWithMaxWidth:(CGFloat)maxWidth
{
if (self.labels.count) {
CGFloat labelHeight = 0.f;
CGFloat width = 0.f;

for (NSUInteger i = 0; i < self.labels.count; i++) {
CGSize size;
if (self.adjustLabel && (i == 0 || i == self.labels.count - 1)) {
width = maxWidth / 2.f + maxRadius;
size = CGSizeMake([self roundForTextDrawing:maxWidth / 2.f + maxRadius], CGFLOAT_MAX);
} else {
width = maxWidth;
size = CGSizeMake([self roundForTextDrawing:maxWidth], CGFLOAT_MAX);
}

CGFloat height = [self.labels[i] boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
CGFloat height = [self.labels[i] boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName : self.labelFont}
context:nil].size.height;
Expand Down Expand Up @@ -512,6 +512,11 @@ - (void)removeLabelLayers
[_trackLabelsArray removeAllObjects];
}

- (CGFloat)roundForTextDrawing:(CGFloat)value
{
return floor(value * [UIScreen mainScreen].scale) / [UIScreen mainScreen].scale;
}

#pragma mark - Access methods

- (void)setIndex:(NSUInteger)index animated:(BOOL)animated
Expand Down

0 comments on commit ccef742

Please sign in to comment.