167 lines
5.6 KiB
Objective-C
167 lines
5.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
|
|
for (UILabel *label in _LSK) {
|
|
label.adjustsFontSizeToFitWidth = YES;
|
|
label.userInteractionEnabled = YES;
|
|
UITapGestureRecognizer *reg =
|
|
[[UITapGestureRecognizer alloc] initWithTarget:self
|
|
action:@selector(enterLSK:)];
|
|
[reg setNumberOfTapsRequired:1];
|
|
[reg setNumberOfTouchesRequired:1];
|
|
[label addGestureRecognizer:reg];
|
|
}
|
|
}
|
|
|
|
- (void)viewDidAppear:(BOOL)animated {
|
|
[self loadScreen:@"Menu"];
|
|
}
|
|
|
|
- (void)viewDidLayoutSubviews {
|
|
// Size and LineBreak
|
|
_Header.font = [self findAdaptiveFontForUILabelSize:_Header.frame.size
|
|
withMinimumSize:5
|
|
withMod:1];
|
|
_Header.adjustsFontSizeToFitWidth = YES;
|
|
_Scratchpad.font = [self findAdaptiveFontForUILabelSize:_Scratchpad.frame.size
|
|
withMinimumSize:5
|
|
withMod:1];
|
|
_Scratchpad.adjustsFontSizeToFitWidth = YES;
|
|
}
|
|
|
|
- (void)clearScreen {
|
|
for (UILabel *label in _LSK) {
|
|
label.text = @"";
|
|
}
|
|
_Scratchpad.text = @"";
|
|
_Header.text = @"";
|
|
}
|
|
|
|
- (void)enterLSK:(UITapGestureRecognizer *)tapReg {
|
|
UILabel *label = ((UILabel *)tapReg.view);
|
|
BOOL append = false;
|
|
NSMutableAttributedString *attSOld = [label.attributedText mutableCopy];
|
|
NSRange rangeDesc = [[attSOld string]
|
|
rangeOfString:[[[attSOld string] componentsSeparatedByString:@"\n"]
|
|
firstObject]];
|
|
NSRange rangeEntry = [[attSOld string]
|
|
rangeOfString:[[[attSOld string] componentsSeparatedByString:@"\n"]
|
|
lastObject]];
|
|
append = rangeDesc.length + 1 == attSOld.length;
|
|
|
|
if ([_Scratchpad.text isEqualToString:@"DELETE"]) {
|
|
if (append) {
|
|
} else {
|
|
NSRange new = NSMakeRange(rangeDesc.length + 1, rangeEntry.length);
|
|
[attSOld replaceCharactersInRange:new withString:@""];
|
|
label.attributedText = attSOld;
|
|
}
|
|
} else {
|
|
if (!append) {
|
|
[attSOld replaceCharactersInRange:NSMakeRange(rangeDesc.length + 1,
|
|
rangeEntry.length)
|
|
withString:_Scratchpad.text];
|
|
[attSOld
|
|
addAttribute:NSFontAttributeName
|
|
value:[self findAdaptiveFontForUILabelSize:label.frame.size
|
|
withMinimumSize:5
|
|
withMod:2]
|
|
range:NSMakeRange(rangeDesc.length + 1, rangeEntry.length)];
|
|
} else {
|
|
NSMutableAttributedString *attSNew =
|
|
[[NSMutableAttributedString alloc] initWithString:_Scratchpad.text];
|
|
[attSNew
|
|
addAttribute:NSFontAttributeName
|
|
value:[self findAdaptiveFontForUILabelSize:label.frame.size
|
|
withMinimumSize:5
|
|
withMod:2]
|
|
range:NSMakeRange(0, attSNew.length)];
|
|
[attSOld appendAttributedString:attSNew];
|
|
}
|
|
label.attributedText = attSOld;
|
|
}
|
|
_Scratchpad.text = @"";
|
|
}
|
|
|
|
- (UIFont *)findAdaptiveFontForUILabelSize:(CGSize)labelSize
|
|
withMinimumSize:(NSInteger)minSize
|
|
withMod:(float)mod {
|
|
UIFont *tempFont = nil;
|
|
NSString *testString =
|
|
@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
NSInteger tempMin = minSize / mod;
|
|
NSInteger tempMax = 256;
|
|
NSInteger mid = 0;
|
|
NSInteger difference = 0;
|
|
|
|
while (tempMin <= tempMax) {
|
|
mid = tempMin + (tempMax - tempMin) / 2;
|
|
tempFont = [UIFont systemFontOfSize:mid];
|
|
difference =
|
|
labelSize.height / mod -
|
|
[testString
|
|
sizeWithAttributes:[NSDictionary
|
|
dictionaryWithObject:tempFont
|
|
forKey:NSFontAttributeName]]
|
|
.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];
|
|
}
|
|
|
|
- (void)loadScreen:(NSString *)screenName {
|
|
_Header.text = screenName;
|
|
NSArray *screen = [[NSArray alloc]
|
|
initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:screenName
|
|
ofType:@"plist"]];
|
|
for (int i = 0; i < _LSK.count; i++) {
|
|
NSMutableAttributedString *attS = [[NSMutableAttributedString alloc]
|
|
initWithString:[NSString stringWithFormat:@"%@\n", screen[i]]];
|
|
NSRange range = [[attS string] rangeOfString:screen[i]];
|
|
[attS addAttribute:NSFontAttributeName
|
|
value:[self findAdaptiveFontForUILabelSize:((UILabel *)_LSK[i])
|
|
.frame.size
|
|
withMinimumSize:5
|
|
withMod:2.5]
|
|
range:range];
|
|
((UILabel *)_LSK[i]).attributedText = attS;
|
|
}
|
|
}
|
|
|
|
@end
|