FMCPlanner2/FMC Planner 2/ViewControllerScreen.m

96 lines
2.6 KiB
Objective-C

//
// ViewControllerScreen.m
// FMC Planner 2
//
// Created by Kilian Hofmann on 20.03.16.
// Copyright © 2016 Kilian Hofmann. All rights reserved.
//
#import "ViewControllerScreen.h"
@implementation ViewControllerScreen
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Init Start Page
[self clearScreen];
_Header.text = @"START";
_Scratchpad.text = @"";
// Tap gesture shit and fitToSize
for (UILabel *label in _LSK) {
label.font = [self findAdaptiveFontForUILabelSize:label.frame.size
withMinimumSize:21];
label.adjustsFontSizeToFitWidth = YES;
label.userInteractionEnabled = YES;
UITapGestureRecognizer *reg =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(enterLSK:)];
[reg setNumberOfTapsRequired:1];
[reg setNumberOfTouchesRequired:1];
[label addGestureRecognizer:reg];
}
_Header.font = [self findAdaptiveFontForUILabelSize:_Header.frame.size
withMinimumSize:21];
_Header.adjustsFontSizeToFitWidth = YES;
_Scratchpad.font = [self findAdaptiveFontForUILabelSize:_Scratchpad.frame.size
withMinimumSize:21];
_Scratchpad.adjustsFontSizeToFitWidth = YES;
}
- (void)clearScreen {
for (UILabel *label in _LSK) {
label.text = @"";
}
_Scratchpad.text = @"";
_Header.text = @"";
}
- (void)enterLSK:(UITapGestureRecognizer *)tapReg {
if ([_Scratchpad.text isEqualToString:@"DELETE"]) {
((UILabel *)tapReg.view).text = @"";
} else {
((UILabel *)tapReg.view).text = _Scratchpad.text;
}
_Scratchpad.text = @"";
}
- (UIFont *)findAdaptiveFontForUILabelSize:(CGSize)labelSize
withMinimumSize:(NSInteger)minSize {
UIFont *tempFont = nil;
NSString *testString =
@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSInteger tempMin = minSize;
NSInteger tempMax = 256;
NSInteger mid = 0;
NSInteger difference = 0;
while (tempMin <= tempMax) {
mid = tempMin + (tempMax - tempMin) / 2;
tempFont = [UIFont systemFontOfSize:mid];
difference = labelSize.height - [testString sizeWithFont:tempFont].height;
if (mid == tempMin || mid == tempMax) {
if (difference < 0) {
return [UIFont systemFontOfSize:(mid - 1)];
}
return [UIFont systemFontOfSize:mid];
}
if (difference < 0) {
tempMax = mid - 1;
} else if (difference > 0) {
tempMin = mid + 1;
} else {
return [UIFont systemFontOfSize:mid];
}
}
return [UIFont systemFontOfSize:mid];
}
@end