Last active
July 26, 2018 18:45
-
-
Save CreatureSurvive/bbc9e6736ed9ca3a31839e5769166758 to your computer and use it in GitHub Desktop.
Custom PSTableCell for displaying numerical values in a formatted manor, also implements a custom ToolBar for the inputAccessoryView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @Author: Dana Buehre <creaturesurvive> | |
| * @Date: 11-09-2017 1:22:14 | |
| * @Email: [email protected] | |
| * @Filename: CSPValueCell.h | |
| * @Last modified by: creaturesurvive | |
| * @Last modified time: 16-09-2017 10:45:22 | |
| * @Copyright: Copyright © 2014-2017 CreatureSurvive | |
| */ | |
| #import <Preferences/PSEditableTableCell.h> | |
| #import <Preferences/PSSpecifier.h> | |
| @interface CSPValueCell : PSEditableTableCell | |
| - (NSString *)formattedValueForValue:(id)value; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @Author: Dana Buehre <creaturesurvive> | |
| * @Date: 10-09-2017 9:08:26 | |
| * @Email: [email protected] | |
| * @Filename: CSPValueCell.m | |
| * @Last modified by: creaturesurvive | |
| * @Last modified time: 16-09-2017 10:57:34 | |
| * @Copyright: Copyright © 2014-2017 CreatureSurvive | |
| */ | |
| #include "CSPValueCell.h" | |
| @implementation CSPValueCell { | |
| NSNumberFormatter *_numberFormatter; | |
| NSMutableDictionary *_accessoryTitlesAndKeys; | |
| UIScrollView *_scrollView; | |
| UIToolbar *_toolbar; | |
| BOOL _formatsDecimals; | |
| BOOL _localeDecimalInvalid; | |
| } | |
| #pragma mark - PSTableCell | |
| - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)identifier specifier:(PSSpecifier *)specifier { | |
| if ((self = [super initWithStyle:style reuseIdentifier:identifier specifier:specifier])) { | |
| _numberFormatter = [NSNumberFormatter new]; | |
| [_numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; | |
| [_numberFormatter setMaximumFractionDigits:4]; | |
| [_numberFormatter setDecimalSeparator:@"."]; | |
| _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)]; | |
| _scrollView.backgroundColor = [UIColor colorWithRed:209.f/255.f green:213.f/255.f blue:219.f/255.f alpha:1.0]; | |
| _scrollView.showsVerticalScrollIndicator = NO; | |
| _scrollView.showsHorizontalScrollIndicator = NO; | |
| _scrollView.hidden = YES; | |
| _toolbar = [[UIToolbar alloc] initWithFrame:_scrollView.bounds]; | |
| [[(UIView *) _toolbar valueForKey:@"_backgroundView"] setHidden:YES]; | |
| _toolbar.tintColor = self.textLabel.textColor; | |
| _toolbar.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; | |
| _toolbar.hidden = YES; | |
| [_scrollView addSubview:_toolbar]; | |
| [self textField].inputAccessoryView = _scrollView; | |
| _localeDecimalInvalid = ![[[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator] isEqualToString:@"."]; | |
| [self refreshCellWithContentsOfSpecifier:specifier]; | |
| } | |
| return self; | |
| } | |
| - (void)refreshCellWithContentsOfSpecifier:(PSSpecifier *)specifier { | |
| _accessoryTitlesAndKeys = [NSMutableDictionary new]; | |
| NSMutableArray *accessoryButtons = [NSMutableArray new]; | |
| // check for accessoryButtons in the specifier | |
| NSArray *specifierButtons = [NSArray new]; | |
| if ((specifierButtons = [specifier propertyForKey:@"accessoryButtons"])) { | |
| for (NSDictionary *item in specifierButtons) { | |
| if ([item[@"separator"] boolValue]) { | |
| [accessoryButtons insertObject:[self accessoryButtonSpace] atIndex:[item[@"index"] integerValue]]; | |
| } else { | |
| [accessoryButtons insertObject:[self accessoryButtonWithTitle:item[@"title"] action:@selector(replaceOrInsertText:)] atIndex:[item[@"index"] integerValue]]; | |
| [_accessoryTitlesAndKeys setObject:item[@"value"] forKey:item[@"title"]]; | |
| } | |
| } | |
| } | |
| // check the keyboard type | |
| _formatsDecimals = [[specifier propertyForKey:@"formatsDecimals"] boolValue]; | |
| if ([[specifier propertyForKey:@"isDecimalWithNegative"] boolValue]) { | |
| [self.textField setKeyboardType:UIKeyboardTypeDecimalPad]; | |
| [accessoryButtons addObject:[self accessoryButtonWithTitle:@"+/-" action:@selector(toggleNegetiveValue)]]; | |
| } | |
| if (_localeDecimalInvalid) { | |
| [_accessoryTitlesAndKeys setObject:@"." forKey:@"."]; | |
| [accessoryButtons addObject:[self accessoryButtonWithTitle:@"." action:@selector(replaceOrInsertText:)]]; | |
| } | |
| // setup toolbar if necessary | |
| if ((accessoryButtons.count)) { | |
| _scrollView.hidden = _toolbar.hidden = NO; | |
| _toolbar.items = [accessoryButtons copy]; | |
| CGRect lastButtonRect = _toolbar.subviews.lastObject.frame; | |
| CGFloat width = lastButtonRect.origin.x + lastButtonRect.size.width + 40; | |
| _toolbar.frame = CGRectMake(0, 0, width, 40); | |
| _scrollView.contentSize = _toolbar.frame.size; | |
| } | |
| } | |
| - (void)setValue:(id)value { | |
| value = _formatsDecimals ? [self formattedValueForValue:value] : value; | |
| [super setValue:value]; | |
| } | |
| #pragma mark - Buttons | |
| - (UIBarButtonItem *)accessoryButtonWithTitle:(NSString *)title action:(SEL)action { | |
| UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; | |
| button.contentEdgeInsets = UIEdgeInsetsMake(5.0f, 10.0f, 5.0f, 10.0f); | |
| button.showsTouchWhenHighlighted = YES; | |
| button.layer.backgroundColor = [UIColor whiteColor].CGColor; | |
| button.layer.cornerRadius = 5.0; | |
| [button setTitle:title forState:UIControlStateNormal]; | |
| [button setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal]; | |
| [button setTitleColor:[[UIColor darkTextColor] colorWithAlphaComponent:0.5] forState:UIControlStateHighlighted]; | |
| [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; | |
| [button sizeToFit]; | |
| return [[UIBarButtonItem alloc] initWithCustomView:button]; | |
| } | |
| - (UIBarButtonItem *)accessoryButtonSpace { | |
| UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; | |
| button.contentEdgeInsets = UIEdgeInsetsMake(-3.0f, 2.0f, -3.0f, 2.0f); | |
| [button setTitle:@"︱" forState:UIControlStateNormal]; | |
| [button setTitleColor:[[UIColor darkTextColor] colorWithAlphaComponent:0.2] forState:UIControlStateNormal]; | |
| [button sizeToFit]; | |
| return [[UIBarButtonItem alloc] initWithCustomView:button]; | |
| } | |
| #pragma mark - Text Manipulation | |
| - (NSString *)formattedValueForValue:(id)value { | |
| NSNumber *numberValue = [NSNumber numberWithFloat:[value floatValue]]; | |
| return [_numberFormatter stringFromNumber:numberValue]; | |
| } | |
| - (void)toggleNegetiveValue { | |
| NSString *value = [self textField].text; | |
| if (value.length > 0) { | |
| NSString *firstCharacter = [value substringToIndex:1]; | |
| self.textField.text = ([firstCharacter isEqualToString:@"-"]) ? [value substringFromIndex:1] : [NSString stringWithFormat:@"-%@", value]; | |
| } | |
| } | |
| - (void)replaceOrInsertText:(UIButton *)item { | |
| NSString *newText = _accessoryTitlesAndKeys[[item currentTitle]]; | |
| [self.textField replaceRange:self.textField.selectedTextRange withText:newText]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment