FMCPlanner2/FMC Planner 2/SharedDeclerations.m

69 lines
1.9 KiB
Objective-C

//
// SharedDecleartions.m
// FMC Planner 2
//
// Created by Kilian Hofmann on 23.03.16.
// Copyright © 2016 Kilian Hofmann. All rights reserved.
//
#import "SharedDeclerations.h"
@implementation SharedDeclerations
#pragma mark - Adaptive font size method
+ (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:@{
NSFontAttributeName : 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];
}
#pragma mark - Savepath method
+ (NSString *)savePathForFile:(NSString *)file
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsPath = paths[0];
return [documentsPath
stringByAppendingString:[NSString stringWithFormat:@"/%@", file]];
}
@end