FMCPlanner2/FMC Planner 2/SharedDeclerations.m

53 lines
1.4 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
+ (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];
}
@end