Classes/ImperialPickerController.m

/*
     File: ImperialPickerController.m
 Abstract: Controller to managed a picker view displaying imperial weights.
 
  Version: 1.5
 
 Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
 Inc. ("Apple") in consideration of your agreement to the following
 terms, and your use, installation, modification or redistribution of
 this Apple software constitutes acceptance of these terms.  If you do
 not agree with these terms, please do not use, install, modify or
 redistribute this Apple software.
 
 In consideration of your agreement to abide by the following terms, and
 subject to these terms, Apple grants you a personal, non-exclusive
 license, under Apple's copyrights in this original Apple software (the
 "Apple Software"), to use, reproduce, modify and redistribute the Apple
 Software, with or without modifications, in source and/or binary forms;
 provided that if you redistribute the Apple Software in its entirety and
 without modifications, you must retain this notice and the following
 text and disclaimers in all such redistributions of the Apple Software.
 Neither the name, trademarks, service marks or logos of Apple Inc. may
 be used to endorse or promote products derived from the Apple Software
 without specific prior written permission from Apple.  Except as
 expressly stated in this notice, no other rights or licenses, express or
 implied, are granted by Apple herein, including but not limited to any
 patent rights that may be infringed by your derivative works or by other
 works in which the Apple Software may be incorporated.
 
 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
 FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
 OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
 
 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
 MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
 STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 
 Copyright (C) 2014 Apple Inc. All Rights Reserved.
 
 */
 
#import "ImperialPickerController.h"
 
@interface ImperialPickerController () <UIPickerViewDataSource, UIPickerViewDelegate>
 
@property (nonatomic, strong) IBOutlet UIPickerView *pickerView;
@property (nonatomic, strong) IBOutlet UILabel *label;
 
@end
 
 
#pragma mark -
 
@implementation ImperialPickerController
 
// Identifiers and widths for the various components
#define POUNDS_COMPONENT 0
#define POUNDS_COMPONENT_WIDTH 110
#define POUNDS_LABEL_WIDTH 60
 
#define OUNCES_COMPONENT 1
#define OUNCES_COMPONENT_WIDTH 106
#define OUNCES_LABEL_WIDTH 56
 
 
// Identifies for component views
#define VIEW_TAG 41
#define SUB_LABEL_TAG 42
#define LABEL_TAG 43
 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 2;
}
 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    // Number of rows depends on the currently-selected unit and the component.
    if (component == POUNDS_COMPONENT) {
        return 29;
    }
    // OUNCES_LABEL_COMPONENT
    return 16;
}
 
- (UIView *)labelCellWithWidth:(CGFloat)width rightOffset:(CGFloat)offset {
    
    // Create a new view that contains a label offset from the right.
    CGRect frame = CGRectMake(0.0, 0.0, width, 32.0);
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.tag = VIEW_TAG;
    
    frame.size.width = width - offset;
    UILabel *subLabel = [[UILabel alloc] initWithFrame:frame];
    subLabel.textAlignment = NSTextAlignmentRight;
    subLabel.backgroundColor = [UIColor clearColor];
    subLabel.font = [UIFont systemFontOfSize:24.0];
    subLabel.userInteractionEnabled = NO;
    
    subLabel.tag = SUB_LABEL_TAG;
    
    [view addSubview:subLabel];
    return view;
}
 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    UIView *returnView = nil;
    
    // Reuse the label if possible, otherwise create and configure a new one.
    if ((view.tag == VIEW_TAG) || (view.tag == LABEL_TAG)) {
        returnView = view;
    }
    else {
        if (component == POUNDS_COMPONENT) {
            returnView = [self labelCellWithWidth:POUNDS_COMPONENT_WIDTH rightOffset:POUNDS_LABEL_WIDTH];
        }
        else {
            returnView = [self labelCellWithWidth:OUNCES_COMPONENT_WIDTH rightOffset:OUNCES_LABEL_WIDTH];
        }
    }
    
    // The text shown in the component is just the number of the component.
    NSString *text = [NSString stringWithFormat:@"%d", row];
    
    // Where to set the text in depends on what sort of view it is.
    UILabel *theLabel = nil;
    if (returnView.tag == VIEW_TAG) {
        theLabel = (UILabel *)[returnView viewWithTag:SUB_LABEL_TAG];
    }
    else {
        theLabel = (UILabel *)returnView;
    }
    
    theLabel.text = text;
    return returnView;
}
 
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    
    if (component == POUNDS_COMPONENT) {
        return POUNDS_COMPONENT_WIDTH;
    }
    // OUNCES_COMPONENT
    return OUNCES_COMPONENT_WIDTH;
}
 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    // If the user chooses a new row, update the label accordingly.
    [self updateLabel];
}
 
- (void)updateLabel {
 
    /*
     If the user has entered imperial units, find the number of pounds and ounces and convert that to kilograms and grams.
     Don't display 0 kg.
     */
    NSInteger ounces = [self.pickerView selectedRowInComponent:OUNCES_COMPONENT];
    ounces += [self.pickerView selectedRowInComponent:POUNDS_COMPONENT] * 16;
    
    float grams = ounces * 28.349;
    if (grams > 1000.0) {
        NSInteger kg = grams / 1000;
        grams -= kg *1000;
        self.label.text = [NSString stringWithFormat:@"%d kg  %1.0f g", kg, grams];
    }
    else {
        self.label.text = [NSString stringWithFormat:@"%1.0f g", grams];
    }
}
    
@end