Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing & Baseline Offset Unit String #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Pod/Classes/MBCircularProgressBarLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@
*/
@property (nonatomic,copy) NSString *unitString;

/**
* A bool indicating the placement of the unit string relative to the value (YES=right, NO=left)
*/
@property (nonatomic, assign) BOOL unitTrailing;

/**
* The offset (in points) of the unit string from the baseline (∞,∞)
*/
@property (nonatomic, assign) CGFloat unitBaselineOffset;


/**
* The color of the value and unit text
*/
Expand Down
8 changes: 6 additions & 2 deletions Pod/Classes/MBCircularProgressBarLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ @implementation MBCircularProgressBarLayer
@dynamic unitFontName;
@dynamic valueFontName;
@dynamic showUnitString;
@dynamic unitTrailing;
@dynamic unitBaselineOffset;
@dynamic showValueString;
@dynamic textOffset;
@dynamic countdown;
Expand Down Expand Up @@ -158,11 +160,13 @@ - (void)drawText:(CGSize)rectSize context:(CGContextRef)c

// ad the unit only if specified
if (self.showUnitString) {
NSDictionary* unitFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: self.unitFontName size:self.unitFontSize == -1 ? rectSize.height/7 : self.unitFontSize], NSForegroundColorAttributeName: self.fontColor, NSParagraphStyleAttributeName: textStyle};
NSDictionary* unitFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: self.unitFontName size:self.unitFontSize == -1 ? rectSize.height/7 : self.unitFontSize], NSForegroundColorAttributeName: self.fontColor, NSParagraphStyleAttributeName: textStyle, NSBaselineOffsetAttributeName: [NSNumber numberWithFloat:self.unitBaselineOffset]};

NSAttributedString* unit =
[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", self.unitString] attributes:unitFontAttributes];
[text appendAttributedString:unit];

// place the unit string before or after the value text
self.unitTrailing ? [text appendAttributedString:unit] : [text insertAttributedString:unit atIndex:0];
}

CGSize percentSize = [text size];
Expand Down
10 changes: 10 additions & 0 deletions Pod/Classes/MBCircularProgressBarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ IB_DESIGNABLE
*/
@property (nonatomic,copy) IBInspectable NSString *unitString;

/**
* A bool indicating the placement of the unit string relative to the value (YES=right, NO=left)
*/
@property (nonatomic, assign) IBInspectable BOOL unitTrailing;

/**
* The offset (in points) of the unit string from the baseline (∞,∞)
*/
@property (nonatomic, assign) IBInspectable CGFloat unitBaselineOffset;

/**
* The color of the value and unit text
*/
Expand Down
18 changes: 18 additions & 0 deletions Pod/Classes/MBCircularProgressBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ -(void)initView:(CGRect)frame{
[self setContentScaleFactor:[[UIScreen mainScreen] scale]];

[self setUnitString:@"%"];
[self setUnitTrailing:YES];
[self setUnitBaselineOffset:0.f];
[self setValue:0.f];
[self setMaxValue:100.f];
[self setProgressRotationAngle:0.f];
Expand Down Expand Up @@ -151,6 +153,22 @@ -(NSString*)unitString{
return self.progressLayer.unitString;
}

-(void)setUnitTrailing:(BOOL)unitTrailing {
self.progressLayer.unitTrailing = unitTrailing;
}

-(BOOL)unitTrailing{
return self.progressLayer.unitTrailing;
}

-(void)setUnitBaselineOffset:(CGFloat)unitBaselineOffset {
self.progressLayer.unitBaselineOffset = unitBaselineOffset;
}

-(CGFloat)unitBaselineOffset{
return self.progressLayer.unitBaselineOffset;
}

-(void)setFontColor:(UIColor*)color{
self.progressLayer.fontColor = color;
}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ valueFontSize | CGFloat | The font size of the value text | [0,∞)
valueFontName | NSString | The name of the font of the unit string | Any valid font name
unitFontSize | CGFloat | The font size of the unit text | [0,∞)
unitString | NSString | The string that represents the units, usually % |
unitTrailing | BOOL | A bool indicating the placement of the unit string relative to the value | {YES=right, NO=left}
unitBaselineOffset | CGFloat | The offset (in points) of the unit string from the baseline | (∞,∞)
fontColor | UIColor | The color of the value and unit text |
decimalPlaces | NSInteger | Number of decimal places of the value | [0,∞)
progressRotationAngle | CGFloat | Progress bar rotation (Clockewise)| [0,100]
Expand Down