From 83298bd5a3bf7803320345a2d41c8b33492f3908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Fri, 13 Jan 2012 06:50:24 +0100 Subject: [PATCH 01/13] Baseline auto-alignment of replacement font and original font --- FontReplacer Demo.xcodeproj/project.pbxproj | 1 + UIFont+Replacement/UIFont+Replacement.m | 24 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/FontReplacer Demo.xcodeproj/project.pbxproj b/FontReplacer Demo.xcodeproj/project.pbxproj index a8395d7..6e47124 100644 --- a/FontReplacer Demo.xcodeproj/project.pbxproj +++ b/FontReplacer Demo.xcodeproj/project.pbxproj @@ -81,6 +81,7 @@ DA9861FB13F01BCE006DEC9A /* Products */, ); sourceTree = ""; + usesTabs = 1; }; DA9861FB13F01BCE006DEC9A /* Products */ = { isa = PBXGroup; diff --git a/UIFont+Replacement/UIFont+Replacement.m b/UIFont+Replacement/UIFont+Replacement.m index 47ca968..ad5f4c7 100644 --- a/UIFont+Replacement/UIFont+Replacement.m +++ b/UIFont+Replacement/UIFont+Replacement.m @@ -30,11 +30,16 @@ + (void) load Method fontWithName_size_traits_ = class_getClassMethod([UIFont class], @selector(fontWithName:size:traits:)); Method replacementFontWithName_size_ = class_getClassMethod([UIFont class], @selector(replacement_fontWithName:size:)); Method replacementFontWithName_size_traits_ = class_getClassMethod([UIFont class], @selector(replacement_fontWithName:size:traits:)); - + if (fontWithName_size_ && replacementFontWithName_size_ && strcmp(method_getTypeEncoding(fontWithName_size_), method_getTypeEncoding(replacementFontWithName_size_)) == 0) method_exchangeImplementations(fontWithName_size_, replacementFontWithName_size_); if (fontWithName_size_traits_ && replacementFontWithName_size_traits_ && strcmp(method_getTypeEncoding(fontWithName_size_traits_), method_getTypeEncoding(replacementFontWithName_size_traits_)) == 0) method_exchangeImplementations(fontWithName_size_traits_, replacementFontWithName_size_traits_); + + Method ascender_ = class_getInstanceMethod([UIFont class], @selector(ascender)); + Method replacementAscender_ = class_getInstanceMethod([UIFont class], @selector(replacement_ascender)); + if (ascender_ && replacementAscender_ && strcmp(method_getTypeEncoding(ascender_), method_getTypeEncoding(replacementAscender_)) == 0) + method_exchangeImplementations(ascender_, replacementAscender_); } + (UIFont *) replacement_fontWithName:(NSString *)fontName size:(CGFloat)fontSize @@ -51,6 +56,23 @@ + (UIFont *) replacement_fontWithName:(NSString *)fontName size:(CGFloat)fontSiz return [self replacement_fontWithName:replacementFontName ?: fontName size:fontSize traits:traits]; } +- (CGFloat)replacement_ascender +{ + NSString *fontName = [self fontName]; + CGFloat ascender = [self replacement_ascender]; + + NSArray *replacedFontNames = [replacementDictionary allKeysForObject:fontName]; + if ([replacedFontNames count] == 0) + { + return ascender; + } + + NSString *replacedFontName = [replacedFontNames objectAtIndex:0]; + NSNumber *traits = [self valueForKey:@"traits"]; + UIFont *replacedFont = [UIFont replacement_fontWithName:replacedFontName size:self.pointSize traits:[traits unsignedIntegerValue]]; + return [replacedFont replacement_ascender]; +} + + (NSDictionary *) replacementDictionary { return replacementDictionary; From f1af4d0ac4669749ea1b417024c1d34e24c63630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Fri, 13 Jan 2012 06:54:25 +0100 Subject: [PATCH 02/13] Remove dictionary instead of calling private method --- UIFont+Replacement/UIFont+Replacement.m | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/UIFont+Replacement/UIFont+Replacement.m b/UIFont+Replacement/UIFont+Replacement.m index ad5f4c7..114d7d7 100644 --- a/UIFont+Replacement/UIFont+Replacement.m +++ b/UIFont+Replacement/UIFont+Replacement.m @@ -68,8 +68,13 @@ - (CGFloat)replacement_ascender } NSString *replacedFontName = [replacedFontNames objectAtIndex:0]; - NSNumber *traits = [self valueForKey:@"traits"]; - UIFont *replacedFont = [UIFont replacement_fontWithName:replacedFontName size:self.pointSize traits:[traits unsignedIntegerValue]]; + + // Trick: To get to the original font, remove the replacement dictionary temporarily + NSDictionary *originalReplacementDictionary = [UIFont replacementDictionary]; + [UIFont setReplacementDictionary:nil]; + UIFont *replacedFont = [UIFont fontWithName:replacedFontName size:self.pointSize]; + [UIFont setReplacementDictionary:originalReplacementDictionary]; + return [replacedFont replacement_ascender]; } From d45a06a3019966e165ebcf507ca7f8791e2f386d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Fri, 13 Jan 2012 14:29:28 +0100 Subject: [PATCH 03/13] Font size adjustment slider --- FontReplacer Demo.xcodeproj/project.pbxproj | 25 + FontReplacer Demo/AdjustmentViewController.h | 43 ++ FontReplacer Demo/AdjustmentViewController.m | 109 ++++ .../AdjustmentViewController.xib | 616 ++++++++++++++++++ FontReplacer Demo/AppDelegate.m | 7 +- FontReplacer Demo/FontsViewController.m | 2 +- FontReplacer Demo/OBSlider/OBSlider.h | 26 + FontReplacer Demo/OBSlider/OBSlider.m | 196 ++++++ UIFont+Replacement/UIFont+Replacement.m | 2 +- 9 files changed, 1022 insertions(+), 4 deletions(-) create mode 100644 FontReplacer Demo/AdjustmentViewController.h create mode 100644 FontReplacer Demo/AdjustmentViewController.m create mode 100644 FontReplacer Demo/AdjustmentViewController.xib create mode 100644 FontReplacer Demo/OBSlider/OBSlider.h create mode 100644 FontReplacer Demo/OBSlider/OBSlider.m diff --git a/FontReplacer Demo.xcodeproj/project.pbxproj b/FontReplacer Demo.xcodeproj/project.pbxproj index 6e47124..d34267b 100644 --- a/FontReplacer Demo.xcodeproj/project.pbxproj +++ b/FontReplacer Demo.xcodeproj/project.pbxproj @@ -7,6 +7,9 @@ objects = { /* Begin PBXBuildFile section */ + 6FB0365D14BFFEC900626500 /* AdjustmentViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6FB0365C14BFFEC900626500 /* AdjustmentViewController.m */; }; + 6FB0366014BFFEF500626500 /* AdjustmentViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6FB0365F14BFFEF500626500 /* AdjustmentViewController.xib */; }; + 6FB0366414C0030200626500 /* OBSlider.m in Sources */ = {isa = PBXBuildFile; fileRef = 6FB0366314C0030200626500 /* OBSlider.m */; }; DA336B6B13F01F5D00A60EA6 /* DemoViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = DA336B6A13F01F5D00A60EA6 /* DemoViewController.xib */; }; DA4C182113F446120015A600 /* FontsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DA4C181F13F446110015A600 /* FontsViewController.m */; }; DA4C182813F4506A0015A600 /* ComparatorViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DA4C182613F450680015A600 /* ComparatorViewController.m */; }; @@ -24,6 +27,11 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 6FB0365B14BFFEC900626500 /* AdjustmentViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AdjustmentViewController.h; sourceTree = ""; }; + 6FB0365C14BFFEC900626500 /* AdjustmentViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AdjustmentViewController.m; sourceTree = ""; }; + 6FB0365F14BFFEF500626500 /* AdjustmentViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = AdjustmentViewController.xib; sourceTree = ""; }; + 6FB0366214C0030200626500 /* OBSlider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OBSlider.h; sourceTree = ""; }; + 6FB0366314C0030200626500 /* OBSlider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OBSlider.m; sourceTree = ""; }; DA336B6A13F01F5D00A60EA6 /* DemoViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = DemoViewController.xib; sourceTree = ""; }; DA4C181E13F446110015A600 /* FontsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FontsViewController.h; sourceTree = ""; }; DA4C181F13F446110015A600 /* FontsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FontsViewController.m; sourceTree = ""; }; @@ -61,6 +69,16 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 6FB0366114C0030200626500 /* OBSlider */ = { + isa = PBXGroup; + children = ( + 6FB0366214C0030200626500 /* OBSlider.h */, + 6FB0366314C0030200626500 /* OBSlider.m */, + ); + name = OBSlider; + path = "FontReplacer Demo/OBSlider"; + sourceTree = ""; + }; DA6C855113F02703007060AF /* CaviarDreams */ = { isa = PBXGroup; children = ( @@ -75,6 +93,7 @@ DA9861EF13F01BCD006DEC9A = { isa = PBXGroup; children = ( + 6FB0366114C0030200626500 /* OBSlider */, DA98621913F01E55006DEC9A /* UIFont+Replacement */, DA98620213F01BCE006DEC9A /* FontReplacer Demo */, DA9861FD13F01BCE006DEC9A /* Frameworks */, @@ -103,6 +122,9 @@ DA98620213F01BCE006DEC9A /* FontReplacer Demo */ = { isa = PBXGroup; children = ( + 6FB0365B14BFFEC900626500 /* AdjustmentViewController.h */, + 6FB0365C14BFFEC900626500 /* AdjustmentViewController.m */, + 6FB0365F14BFFEF500626500 /* AdjustmentViewController.xib */, DA98620B13F01BCE006DEC9A /* AppDelegate.h */, DA98620C13F01BCE006DEC9A /* AppDelegate.m */, DA336B6A13F01F5D00A60EA6 /* DemoViewController.xib */, @@ -195,6 +217,7 @@ DA6C855913F02703007060AF /* CaviarDreams_BoldItalic.ttf in Resources */, DA6C855A13F02703007060AF /* CaviarDreams_Italic.ttf in Resources */, DA6C856513F035EA007060AF /* Icon@2x.png in Resources */, + 6FB0366014BFFEF500626500 /* AdjustmentViewController.xib in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -211,6 +234,8 @@ DA4C182113F446120015A600 /* FontsViewController.m in Sources */, DA4C182813F4506A0015A600 /* ComparatorViewController.m in Sources */, DADAB65A13F5DDB0000301E6 /* ComparisonResult.m in Sources */, + 6FB0365D14BFFEC900626500 /* AdjustmentViewController.m in Sources */, + 6FB0366414C0030200626500 /* OBSlider.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/FontReplacer Demo/AdjustmentViewController.h b/FontReplacer Demo/AdjustmentViewController.h new file mode 100644 index 0000000..c806b72 --- /dev/null +++ b/FontReplacer Demo/AdjustmentViewController.h @@ -0,0 +1,43 @@ +// +// AdjustmentViewController.h +// FontReplacer Demo +// +// Created by Samuel Défago on 13.01.12. +// Copyright (c) 2012 Cédric Lüthi. All rights reserved. +// + +#import + +#import "OBSlider.h" + +@interface AdjustmentViewController : UIViewController +{ +@private + UILabel *m_font1NormalLabel; + UILabel *m_font2NormalLabel; + UILabel *m_font1ItalicLabel; + UILabel *m_font2ItalicLabel; + UILabel *m_font1BoldLabel; + UILabel *m_font2BoldLabel; + UILabel *m_font1BoldItalicLabel; + UILabel *m_font2BoldItalicLabel; + OBSlider *m_factorSlider; + UISlider *m_pointSizeSlider; +} + +@property (nonatomic, retain) IBOutlet UILabel *font1NormalLabel; +@property (nonatomic, retain) IBOutlet UILabel *font2NormalLabel; +@property (nonatomic, retain) IBOutlet UILabel *font1ItalicLabel; +@property (nonatomic, retain) IBOutlet UILabel *font2ItalicLabel; +@property (nonatomic, retain) IBOutlet UILabel *font1BoldLabel; +@property (nonatomic, retain) IBOutlet UILabel *font2BoldLabel; +@property (nonatomic, retain) IBOutlet UILabel *font1BoldItalicLabel; +@property (nonatomic, retain) IBOutlet UILabel *font2BoldItalicLabel; + +@property (nonatomic, retain) IBOutlet OBSlider *factorSlider; +@property (nonatomic, retain) IBOutlet UISlider *pointSizeSlider; + +- (IBAction)factorChanged:(id)sender; +- (IBAction)pointSizeChanged:(id)sender; + +@end diff --git a/FontReplacer Demo/AdjustmentViewController.m b/FontReplacer Demo/AdjustmentViewController.m new file mode 100644 index 0000000..c147ed8 --- /dev/null +++ b/FontReplacer Demo/AdjustmentViewController.m @@ -0,0 +1,109 @@ +// +// AdjustmentViewController.m +// FontReplacer Demo +// +// Created by Samuel Défago on 13.01.12. +// Copyright (c) 2012 Cédric Lüthi. All rights reserved. +// + +#import "AdjustmentViewController.h" + +#import "UIFont+Replacement.h" + +@interface AdjustmentViewController () + +- (void) releaseViews; + +- (void) reloadData; + +@end + +@implementation AdjustmentViewController + +@synthesize font1NormalLabel = m_font1NormalLabel; +@synthesize font2NormalLabel = m_font2NormalLabel; +@synthesize font1ItalicLabel = m_font1ItalicLabel; +@synthesize font2ItalicLabel = m_font2ItalicLabel; +@synthesize font1BoldLabel = m_font1BoldLabel; +@synthesize font2BoldLabel = m_font2BoldLabel; +@synthesize font1BoldItalicLabel = m_font1BoldItalicLabel; +@synthesize font2BoldItalicLabel = m_font2BoldItalicLabel; + +@synthesize factorSlider = m_factorSlider; +@synthesize pointSizeSlider = m_pointSizeSlider; + +// MARK: - Object creation and destruction + +- (id) init +{ + if (!(self = [super initWithNibName:@"AdjustmentViewController" bundle:nil])) + return nil; + + return self; +} + +- (void) dealloc +{ + [self releaseViews]; + [super dealloc]; +} + +- (void) releaseViews +{ + self.font1NormalLabel = nil; + self.font2NormalLabel = nil; + self.font1ItalicLabel = nil; + self.font2ItalicLabel = nil; + self.font1BoldLabel = nil; + self.font2BoldLabel = nil; + self.font1BoldItalicLabel = nil; + self.font2BoldItalicLabel = nil; + self.factorSlider = nil; + self.pointSizeSlider = nil; +} + +// MARK: - View lifecycle + +- (void) viewDidLoad +{ + [super viewDidLoad]; + [self reloadData]; +} + +- (void) viewDidUnload +{ + [super viewDidUnload]; + [self releaseViews]; +} + +// MARK: - Reloading screen + +- (void) reloadData +{ + self.font1NormalLabel.font = [UIFont fontWithName:@"CaviarDreams" size:floorf(self.pointSizeSlider.value)]; + self.font1ItalicLabel.font = [UIFont fontWithName:@"CaviarDreams-Italic" size:floorf(self.pointSizeSlider.value)]; + self.font1BoldLabel.font = [UIFont fontWithName:@"CaviarDreams-Bold" size:floorf(self.pointSizeSlider.value)]; + self.font1BoldItalicLabel.font = [UIFont fontWithName:@"CaviarDreams-BoldItalic" size:floorf(self.pointSizeSlider.value)]; + + NSDictionary *originalReplacementDictionary = [UIFont replacementDictionary]; + [UIFont setReplacementDictionary:nil]; + self.font2NormalLabel.font = [UIFont fontWithName:@"ArialMT" size:floorf(self.pointSizeSlider.value)]; + self.font2ItalicLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:floorf(self.pointSizeSlider.value)]; + self.font2BoldLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:floorf(self.pointSizeSlider.value)]; + self.font2BoldItalicLabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:floorf(self.pointSizeSlider.value)]; + [UIFont setReplacementDictionary:originalReplacementDictionary]; +} + +// MARK: - Event callbacks + +- (IBAction)factorChanged:(id)sender +{ + [self reloadData]; +} + +- (IBAction)pointSizeChanged:(id)sender +{ + [self reloadData]; +} + +@end diff --git a/FontReplacer Demo/AdjustmentViewController.xib b/FontReplacer Demo/AdjustmentViewController.xib new file mode 100644 index 0000000..3eed9b7 --- /dev/null +++ b/FontReplacer Demo/AdjustmentViewController.xib @@ -0,0 +1,616 @@ + + + + 1280 + 11C74 + 1938 + 1138.23 + 567.00 + + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + 933 + + + IBUISlider + IBUIView + IBUILabel + IBProxyObject + + + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + + + PluginDependencyRecalculationVersion + + + + + IBFilesOwner + IBCocoaTouchFramework + + + IBFirstResponder + IBCocoaTouchFramework + + + + 274 + + + + 292 + {{20, 8}, {280, 70}} + + + + NO + YES + 0.60000002384185791 + 7 + NO + IBCocoaTouchFramework + Sphinx + + 1 + MC4xOTYwNzg0MzQ2IDAuMzA5ODAzOTMyOSAwLjUyMTU2ODY1NgA + + + 1 + 12 + 1 + + ArialMT + Arial + 0 + 18 + + + ArialMT + 18 + 16 + + + + + 292 + {{20, 8}, {280, 70}} + + + + NO + YES + 7 + NO + IBCocoaTouchFramework + Sphinx + + 1 + MCAwIDAAA + + + 1 + 12 + 1 + + + + + + 292 + {{20, 86}, {280, 70}} + + + + NO + YES + 0.60000002384185791 + 7 + NO + IBCocoaTouchFramework + Sphinx + + + 1 + 12 + 1 + + Arial-ItalicMT + Arial + 1 + 18 + + + Arial-ItalicMT + 18 + 16 + + + + + 292 + {{20, 86}, {280, 70}} + + + + NO + YES + 7 + NO + IBCocoaTouchFramework + Sphinx + + + 1 + 12 + 1 + + + + + + 292 + {{20, 164}, {280, 70}} + + + + NO + YES + 0.60000002384185791 + 7 + NO + IBCocoaTouchFramework + Sphinx + + + 1 + 12 + 1 + + Arial-BoldMT + Arial + 2 + 18 + + + Arial-BoldMT + 18 + 16 + + + + + 292 + {{20, 164}, {280, 70}} + + + + NO + YES + 7 + NO + IBCocoaTouchFramework + Sphinx + + + 1 + 12 + 1 + + + + + + 292 + {{20, 242}, {280, 70}} + + + + NO + YES + 0.60000002384185791 + 7 + NO + IBCocoaTouchFramework + Sphinx + + + 1 + 12 + 1 + + Arial-BoldItalicMT + Arial + 3 + 18 + + + Arial-BoldItalicMT + 18 + 16 + + + + + 292 + {{20, 242}, {280, 70}} + + + + NO + YES + 7 + NO + IBCocoaTouchFramework + Sphinx + + + 1 + 12 + 1 + + + + + + 292 + {{18, 320}, {278, 23}} + + + + _NS:623 + NO + IBCocoaTouchFramework + 0 + 0 + 0.5 + + + + 292 + {{18, 369}, {278, 23}} + + + + _NS:623 + NO + IBCocoaTouchFramework + 0 + 0 + 18 + 5 + 60 + + + {{0, 20}, {320, 411}} + + + + + 3 + MC43NQA + + 2 + + + NO + + + IBCocoaTouchFramework + + + + + + + view + + + + 16 + + + + factorSlider + + + + 29 + + + + pointSizeSlider + + + + 30 + + + + font1NormalLabel + + + + 33 + + + + font2NormalLabel + + + + 34 + + + + font1ItalicLabel + + + + 35 + + + + font2ItalicLabel + + + + 36 + + + + font1BoldLabel + + + + 37 + + + + font2BoldLabel + + + + 38 + + + + font1BoldItalicLabel + + + + 39 + + + + font2BoldItalicLabel + + + + 40 + + + + factorChanged: + + + 13 + + 31 + + + + pointSizeChanged: + + + 13 + + 32 + + + + + + 0 + + + + + + -1 + + + File's Owner + + + -2 + + + + + 4 + + + + + + + + + + + + + + + + + 6 + + + + + 7 + + + + + 8 + + + + + 9 + + + + + 10 + + + + + 11 + + + + + 12 + + + + + 13 + + + + + 14 + + + + + 15 + + + + + + + AdjustmentViewController + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + UIResponder + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + OBSlider + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + + + + + + 40 + + + + + AdjustmentViewController + UIViewController + + id + id + + + + factorChanged: + id + + + pointSizeChanged: + id + + + + OBSlider + UILabel + UILabel + UILabel + UILabel + UILabel + UILabel + UILabel + UILabel + UISlider + + + + factorSlider + OBSlider + + + font1BoldItalicLabel + UILabel + + + font1BoldLabel + UILabel + + + font1ItalicLabel + UILabel + + + font1NormalLabel + UILabel + + + font2BoldItalicLabel + UILabel + + + font2BoldLabel + UILabel + + + font2ItalicLabel + UILabel + + + font2NormalLabel + UILabel + + + pointSizeSlider + UISlider + + + + IBProjectSource + ./Classes/AdjustmentViewController.h + + + + OBSlider + UISlider + + IBProjectSource + ./Classes/OBSlider.h + + + + + 0 + IBCocoaTouchFramework + YES + 3 + 933 + + diff --git a/FontReplacer Demo/AppDelegate.m b/FontReplacer Demo/AppDelegate.m index a347c3e..f050f1d 100644 --- a/FontReplacer Demo/AppDelegate.m +++ b/FontReplacer Demo/AppDelegate.m @@ -8,6 +8,7 @@ #import "AppDelegate.h" +#import "AdjustmentViewController.h" #import "FontsViewController.h" #import "UIFont+Replacement.h" @@ -19,12 +20,14 @@ - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions: { UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UIViewController *demoViewController = [[UIViewController alloc] initWithNibName:@"DemoViewController" bundle:nil]; - UIViewController *fontsViewController = [[FontsViewController alloc] init]; + UIViewController *fontsViewController = [[[FontsViewController alloc] init] autorelease]; + UIViewController *adjustmentViewController = [[[AdjustmentViewController alloc] init] autorelease]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:fontsViewController]; UITabBarController *tabBarController = [[UITabBarController alloc] init]; tabBarController.delegate = self; - tabBarController.viewControllers = [NSArray arrayWithObjects:demoViewController, navigationController, nil]; + tabBarController.viewControllers = [NSArray arrayWithObjects:demoViewController, adjustmentViewController, navigationController, nil]; demoViewController.title = @"Demo"; + adjustmentViewController.title = @"Adjust"; fontsViewController.title = @"Fonts"; if ([window respondsToSelector:@selector(setRootViewController:)]) window.rootViewController = tabBarController; diff --git a/FontReplacer Demo/FontsViewController.m b/FontReplacer Demo/FontsViewController.m index 635149d..eefd393 100644 --- a/FontReplacer Demo/FontsViewController.m +++ b/FontReplacer Demo/FontsViewController.m @@ -13,7 +13,7 @@ @implementation FontsViewController - (id) init { - if (!(self = [self initWithStyle:UITableViewStylePlain])) + if (!(self = [super initWithStyle:UITableViewStylePlain])) return nil; familyNames = [[[UIFont familyNames] sortedArrayUsingSelector:@selector(localizedCompare:)] retain]; diff --git a/FontReplacer Demo/OBSlider/OBSlider.h b/FontReplacer Demo/OBSlider/OBSlider.h new file mode 100644 index 0000000..619836d --- /dev/null +++ b/FontReplacer Demo/OBSlider/OBSlider.h @@ -0,0 +1,26 @@ +// +// OBSlider.h +// +// Created by Ole Begemann on 02.01.11. +// Copyright 2011 Ole Begemann. All rights reserved. +// + +#import + + +@interface OBSlider : UISlider +{ + float scrubbingSpeed; + NSArray *scrubbingSpeeds; + NSArray *scrubbingSpeedChangePositions; + + CGPoint beganTrackingLocation; + + float realPositionValue; +} + +@property (assign, readonly) float scrubbingSpeed; +@property (retain) NSArray *scrubbingSpeeds; +@property (retain) NSArray *scrubbingSpeedChangePositions; + +@end diff --git a/FontReplacer Demo/OBSlider/OBSlider.m b/FontReplacer Demo/OBSlider/OBSlider.m new file mode 100644 index 0000000..90d7a57 --- /dev/null +++ b/FontReplacer Demo/OBSlider/OBSlider.m @@ -0,0 +1,196 @@ +// +// OBSlider.m +// +// Created by Ole Begemann on 02.01.11. +// Copyright 2011 Ole Begemann. All rights reserved. +// + +#import "OBSlider.h" + + +@interface OBSlider () + +@property (assign, readwrite) float scrubbingSpeed; +@property (assign) CGPoint beganTrackingLocation; + +- (NSUInteger) indexOfLowerScrubbingSpeed:(NSArray*)scrubbingSpeedPositions forOffset:(CGFloat)verticalOffset; +- (NSArray *) defaultScrubbingSpeeds; +- (NSArray *) defaultScrubbingSpeedChangePositions; + +@end + + + +@implementation OBSlider + +@synthesize scrubbingSpeed; +@synthesize scrubbingSpeeds; +@synthesize scrubbingSpeedChangePositions; +@synthesize beganTrackingLocation; + + +- (void) dealloc +{ + self.scrubbingSpeeds = nil; + self.scrubbingSpeedChangePositions = nil; + [super dealloc]; +} + + +- (id) initWithFrame:(CGRect)frame +{ + self = [super initWithFrame:frame]; + if (self != nil) + { + self.scrubbingSpeeds = [self defaultScrubbingSpeeds]; + self.scrubbingSpeedChangePositions = [self defaultScrubbingSpeedChangePositions]; + self.scrubbingSpeed = [[self.scrubbingSpeeds objectAtIndex:0] floatValue]; + } + return self; +} + + + +#pragma mark - +#pragma mark NSCoding + +- (id) initWithCoder:(NSCoder *)decoder +{ + self = [super initWithCoder:decoder]; + if (self != nil) + { + if ([decoder containsValueForKey:@"scrubbingSpeeds"]) { + self.scrubbingSpeeds = [decoder decodeObjectForKey:@"scrubbingSpeeds"]; + } else { + self.scrubbingSpeeds = [self defaultScrubbingSpeeds]; + } + + if ([decoder containsValueForKey:@"scrubbingSpeedChangePositions"]) { + self.scrubbingSpeedChangePositions = [decoder decodeObjectForKey:@"scrubbingSpeedChangePositions"]; + } else { + self.scrubbingSpeedChangePositions = [self defaultScrubbingSpeedChangePositions]; + } + + self.scrubbingSpeed = [[self.scrubbingSpeeds objectAtIndex:0] floatValue]; + } + return self; +} + + +- (void) encodeWithCoder:(NSCoder *)coder +{ + [super encodeWithCoder:coder]; + + [coder encodeObject:self.scrubbingSpeeds forKey:@"scrubbingSpeeds"]; + [coder encodeObject:self.scrubbingSpeedChangePositions forKey:@"scrubbingSpeedChangePositions"]; + + // No need to archive self.scrubbingSpeed as it is calculated from the arrays on init +} + + + +#pragma mark - +#pragma mark Touch tracking + +- (BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event +{ + BOOL beginTracking = [super beginTrackingWithTouch:touch withEvent:event]; + if (beginTracking) + { + self.beganTrackingLocation = [touch locationInView:self]; + realPositionValue = self.value; + } + return beginTracking; +} + + +- (BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event +{ + if (self.tracking) + { + CGPoint previousLocation = [touch previousLocationInView:self]; + CGPoint currentLocation = [touch locationInView:self]; + CGFloat trackingOffset = currentLocation.x - previousLocation.x; + + // Find the scrubbing speed that curresponds to the touch's vertical offset + CGFloat verticalOffset = fabsf(currentLocation.y - self.beganTrackingLocation.y); + NSUInteger scrubbingSpeedChangePosIndex = [self indexOfLowerScrubbingSpeed:self.scrubbingSpeedChangePositions forOffset:verticalOffset]; + if (scrubbingSpeedChangePosIndex == NSNotFound) { + scrubbingSpeedChangePosIndex = [self.scrubbingSpeeds count]; + } + self.scrubbingSpeed = [[self.scrubbingSpeeds objectAtIndex:scrubbingSpeedChangePosIndex - 1] floatValue]; + + CGRect trackRect = [self trackRectForBounds:self.bounds]; + realPositionValue = realPositionValue + (self.maximumValue - self.minimumValue) * (trackingOffset / trackRect.size.width); + if ( ((self.beganTrackingLocation.y < currentLocation.y) && (currentLocation.y < previousLocation.y)) || + ((self.beganTrackingLocation.y > currentLocation.y) && (currentLocation.y > previousLocation.y)) ) + { + // We are getting closer to the slider, go closer to the real location + self.value = self.value + self.scrubbingSpeed * (self.maximumValue - self.minimumValue) * (trackingOffset / trackRect.size.width) + (realPositionValue - self.value) / ( 1 + fabsf(currentLocation.y - self.beganTrackingLocation.y)); + } else { + self.value = self.value + self.scrubbingSpeed * (self.maximumValue - self.minimumValue) * (trackingOffset / trackRect.size.width); + } + + if (self.continuous) { + [self sendActionsForControlEvents:UIControlEventValueChanged]; + } + } + return self.tracking; +} + + +- (void) endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event +{ + if (self.tracking) + { + self.scrubbingSpeed = [[self.scrubbingSpeeds objectAtIndex:0] floatValue]; + [self sendActionsForControlEvents:UIControlEventValueChanged]; + } +} + + + +#pragma mark - +#pragma mark Helper methods + +// Return the lowest index in the array of numbers passed in scrubbingSpeedPositions +// whose value is smaller than verticalOffset. +- (NSUInteger) indexOfLowerScrubbingSpeed:(NSArray*)scrubbingSpeedPositions forOffset:(CGFloat)verticalOffset +{ + for (NSUInteger i = 0; i < [scrubbingSpeedPositions count]; i++) { + NSNumber *scrubbingSpeedOffset = [scrubbingSpeedPositions objectAtIndex:i]; + if (verticalOffset < [scrubbingSpeedOffset floatValue]) { + return i; + } + } + return NSNotFound; +} + + + +#pragma mark - +#pragma mark Default values + +// Used in -initWithFrame: and -initWithCoder: +- (NSArray *) defaultScrubbingSpeeds +{ + return [NSArray arrayWithObjects: + [NSNumber numberWithFloat:1.0f], + [NSNumber numberWithFloat:0.5f], + [NSNumber numberWithFloat:0.25f], + [NSNumber numberWithFloat:0.1f], + nil]; +} + + +- (NSArray *) defaultScrubbingSpeedChangePositions +{ + return [NSArray arrayWithObjects: + [NSNumber numberWithFloat:0.0f], + [NSNumber numberWithFloat:50.0f], + [NSNumber numberWithFloat:100.0f], + [NSNumber numberWithFloat:150.0f], + nil]; +} + +@end diff --git a/UIFont+Replacement/UIFont+Replacement.m b/UIFont+Replacement/UIFont+Replacement.m index 114d7d7..429ed1e 100644 --- a/UIFont+Replacement/UIFont+Replacement.m +++ b/UIFont+Replacement/UIFont+Replacement.m @@ -56,7 +56,7 @@ + (UIFont *) replacement_fontWithName:(NSString *)fontName size:(CGFloat)fontSiz return [self replacement_fontWithName:replacementFontName ?: fontName size:fontSize traits:traits]; } -- (CGFloat)replacement_ascender +- (CGFloat) replacement_ascender { NSString *fontName = [self fontName]; CGFloat ascender = [self replacement_ascender]; From 3da06d18e026bad5492e5f353bc1d104278b74ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Fri, 13 Jan 2012 15:59:35 +0100 Subject: [PATCH 04/13] Offset adjustments --- FontReplacer Demo/AdjustmentViewController.h | 5 +- FontReplacer Demo/AdjustmentViewController.m | 57 +++++---- .../AdjustmentViewController.xib | 111 +++++++++--------- FontReplacer Demo/AppDelegate.m | 13 +- UIFont+Replacement/UIFont+Replacement.h | 3 + UIFont+Replacement/UIFont+Replacement.m | 46 +++++++- 6 files changed, 147 insertions(+), 88 deletions(-) diff --git a/FontReplacer Demo/AdjustmentViewController.h b/FontReplacer Demo/AdjustmentViewController.h index c806b72..afd5163 100644 --- a/FontReplacer Demo/AdjustmentViewController.h +++ b/FontReplacer Demo/AdjustmentViewController.h @@ -34,10 +34,9 @@ @property (nonatomic, retain) IBOutlet UILabel *font1BoldItalicLabel; @property (nonatomic, retain) IBOutlet UILabel *font2BoldItalicLabel; -@property (nonatomic, retain) IBOutlet OBSlider *factorSlider; +@property (nonatomic, retain) IBOutlet OBSlider *offsetSlider; @property (nonatomic, retain) IBOutlet UISlider *pointSizeSlider; -- (IBAction)factorChanged:(id)sender; -- (IBAction)pointSizeChanged:(id)sender; +- (IBAction) settingsChanged:(id)sender; @end diff --git a/FontReplacer Demo/AdjustmentViewController.m b/FontReplacer Demo/AdjustmentViewController.m index c147ed8..206ae92 100644 --- a/FontReplacer Demo/AdjustmentViewController.m +++ b/FontReplacer Demo/AdjustmentViewController.m @@ -29,7 +29,7 @@ @implementation AdjustmentViewController @synthesize font1BoldItalicLabel = m_font1BoldItalicLabel; @synthesize font2BoldItalicLabel = m_font2BoldItalicLabel; -@synthesize factorSlider = m_factorSlider; +@synthesize offsetSlider = m_factorSlider; @synthesize pointSizeSlider = m_pointSizeSlider; // MARK: - Object creation and destruction @@ -50,15 +50,15 @@ - (void) dealloc - (void) releaseViews { - self.font1NormalLabel = nil; - self.font2NormalLabel = nil; - self.font1ItalicLabel = nil; - self.font2ItalicLabel = nil; - self.font1BoldLabel = nil; - self.font2BoldLabel = nil; - self.font1BoldItalicLabel = nil; - self.font2BoldItalicLabel = nil; - self.factorSlider = nil; + self.font1NormalLabel = nil; + self.font2NormalLabel = nil; + self.font1ItalicLabel = nil; + self.font2ItalicLabel = nil; + self.font1BoldLabel = nil; + self.font2BoldLabel = nil; + self.font1BoldItalicLabel = nil; + self.font2BoldItalicLabel = nil; + self.offsetSlider = nil; self.pointSizeSlider = nil; } @@ -67,6 +67,9 @@ - (void) releaseViews - (void) viewDidLoad { [super viewDidLoad]; + + self.pointSizeSlider.value = self.font1NormalLabel.font.pointSize; + [self reloadData]; } @@ -79,29 +82,37 @@ - (void) viewDidUnload // MARK: - Reloading screen - (void) reloadData -{ - self.font1NormalLabel.font = [UIFont fontWithName:@"CaviarDreams" size:floorf(self.pointSizeSlider.value)]; - self.font1ItalicLabel.font = [UIFont fontWithName:@"CaviarDreams-Italic" size:floorf(self.pointSizeSlider.value)]; - self.font1BoldLabel.font = [UIFont fontWithName:@"CaviarDreams-Bold" size:floorf(self.pointSizeSlider.value)]; - self.font1BoldItalicLabel.font = [UIFont fontWithName:@"CaviarDreams-BoldItalic" size:floorf(self.pointSizeSlider.value)]; - +{ + NSMutableDictionary *offsetDictionary = [NSMutableDictionary dictionary]; + [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"ArialMT"]; + [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"Arial-ItalicMT"]; + [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"Arial-BoldMT"]; + [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"Arial-BoldItalicMT"]; + [UIFont setOffsetDictionary:[NSDictionary dictionaryWithDictionary:offsetDictionary]]; + NSDictionary *originalReplacementDictionary = [UIFont replacementDictionary]; [UIFont setReplacementDictionary:nil]; + self.font1NormalLabel.font = [UIFont fontWithName:@"ArialMT" size:floorf(self.pointSizeSlider.value)]; + self.font1ItalicLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:floorf(self.pointSizeSlider.value)]; + self.font1BoldLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:floorf(self.pointSizeSlider.value)]; + self.font1BoldItalicLabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:floorf(self.pointSizeSlider.value)]; + [UIFont setReplacementDictionary:originalReplacementDictionary]; + self.font2NormalLabel.font = [UIFont fontWithName:@"ArialMT" size:floorf(self.pointSizeSlider.value)]; self.font2ItalicLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:floorf(self.pointSizeSlider.value)]; self.font2BoldLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:floorf(self.pointSizeSlider.value)]; self.font2BoldItalicLabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:floorf(self.pointSizeSlider.value)]; - [UIFont setReplacementDictionary:originalReplacementDictionary]; + + // Force a refresh + [self.font2NormalLabel setNeedsDisplay]; + [self.font2ItalicLabel setNeedsDisplay]; + [self.font2BoldLabel setNeedsDisplay]; + [self.font2BoldItalicLabel setNeedsDisplay]; } // MARK: - Event callbacks -- (IBAction)factorChanged:(id)sender -{ - [self reloadData]; -} - -- (IBAction)pointSizeChanged:(id)sender +- (IBAction) settingsChanged:(id)sender { [self reloadData]; } diff --git a/FontReplacer Demo/AdjustmentViewController.xib b/FontReplacer Demo/AdjustmentViewController.xib index 3eed9b7..4098548 100644 --- a/FontReplacer Demo/AdjustmentViewController.xib +++ b/FontReplacer Demo/AdjustmentViewController.xib @@ -64,7 +64,7 @@ 0 18 - + ArialMT 18 16 @@ -92,7 +92,7 @@ 12 1 - + @@ -119,7 +119,7 @@ 1 18 - + Arial-ItalicMT 18 16 @@ -144,7 +144,7 @@ 12 1 - + @@ -171,7 +171,7 @@ 2 18 - + Arial-BoldMT 18 16 @@ -196,7 +196,7 @@ 12 1 - + @@ -223,7 +223,7 @@ 3 18 - + Arial-BoldItalicMT 18 16 @@ -248,7 +248,7 @@ 12 1 - + @@ -262,7 +262,7 @@ IBCocoaTouchFramework 0 0 - 0.5 + -1 @@ -276,8 +276,8 @@ IBCocoaTouchFramework 0 0 - 18 - 5 + 33 + 6 60 @@ -308,14 +308,6 @@ 16 - - - factorSlider - - - - 29 - pointSizeSlider @@ -324,22 +316,6 @@ 30 - - - font1NormalLabel - - - - 33 - - - - font2NormalLabel - - - - 34 - font1ItalicLabel @@ -388,23 +364,47 @@ 40 + + + offsetSlider + + + + 41 + + + + font2NormalLabel + + + + 34 + + + + font1NormalLabel + + + + 33 + - factorChanged: + settingsChanged: 13 - 31 + 42 - pointSizeChanged: + settingsChanged: 13 - 32 + 43 @@ -517,29 +517,25 @@ - 40 + 45 AdjustmentViewController UIViewController - - id - id - - - - factorChanged: - id - - - pointSizeChanged: + + settingsChanged: + id + + + settingsChanged: + + settingsChanged: id - + - OBSlider UILabel UILabel UILabel @@ -548,13 +544,10 @@ UILabel UILabel UILabel + OBSlider UISlider - - factorSlider - OBSlider - font1BoldItalicLabel UILabel @@ -587,6 +580,10 @@ font2NormalLabel UILabel + + offsetSlider + OBSlider + pointSizeSlider UISlider diff --git a/FontReplacer Demo/AppDelegate.m b/FontReplacer Demo/AppDelegate.m index f050f1d..d10923e 100644 --- a/FontReplacer Demo/AppDelegate.m +++ b/FontReplacer Demo/AppDelegate.m @@ -39,15 +39,20 @@ - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions: - (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { - if (tabBarController.selectedIndex == 0) + if ([viewController isKindOfClass:[FontsViewController class]]) { self.originalReplacementDictionary = [UIFont replacementDictionary]; - [UIFont setReplacementDictionary:nil]; + [UIFont setReplacementDictionary:nil]; } - else + else { - [UIFont setReplacementDictionary:self.originalReplacementDictionary]; + if (self.originalReplacementDictionary) + { + [UIFont setReplacementDictionary:self.originalReplacementDictionary]; + self.originalReplacementDictionary = nil; + } } + return YES; } diff --git a/UIFont+Replacement/UIFont+Replacement.h b/UIFont+Replacement/UIFont+Replacement.h index 8511d4e..48aaa60 100644 --- a/UIFont+Replacement/UIFont+Replacement.h +++ b/UIFont+Replacement/UIFont+Replacement.h @@ -13,4 +13,7 @@ + (NSDictionary *) replacementDictionary; + (void) setReplacementDictionary:(NSDictionary *)aReplacementDictionary; ++ (NSDictionary *) offsetDictionary; ++ (void) setOffsetDictionary:(NSDictionary *)anOffsetDictionary; + @end diff --git a/UIFont+Replacement/UIFont+Replacement.m b/UIFont+Replacement/UIFont+Replacement.m index 429ed1e..8e55ae8 100644 --- a/UIFont+Replacement/UIFont+Replacement.m +++ b/UIFont+Replacement/UIFont+Replacement.m @@ -12,6 +12,7 @@ @implementation UIFont (Replacement) static NSDictionary *replacementDictionary = nil; +static NSDictionary *offsetDictionary = nil; static void initializeReplacementFonts() { @@ -22,6 +23,9 @@ static void initializeReplacementFonts() NSDictionary *replacementDictionary = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"ReplacementFonts"]; [UIFont setReplacementDictionary:replacementDictionary]; + + NSDictionary *offsetDictionary = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"ReplacementOffsets"]; + [UIFont setOffsetDictionary:offsetDictionary]; } + (void) load @@ -42,6 +46,16 @@ + (void) load method_exchangeImplementations(ascender_, replacementAscender_); } ++ (CGFloat) offsetForFontWithName:(NSString *)fontName +{ + if (!offsetDictionary) + { + return 0.f; + } + + return [[offsetDictionary objectForKey:fontName] floatValue]; +} + + (UIFont *) replacement_fontWithName:(NSString *)fontName size:(CGFloat)fontSize { initializeReplacementFonts(); @@ -75,7 +89,7 @@ - (CGFloat) replacement_ascender UIFont *replacedFont = [UIFont fontWithName:replacedFontName size:self.pointSize]; [UIFont setReplacementDictionary:originalReplacementDictionary]; - return [replacedFont replacement_ascender]; + return [replacedFont replacement_ascender] + self.pointSize * [UIFont offsetForFontWithName:replacedFontName]; } + (NSDictionary *) replacementDictionary @@ -116,4 +130,34 @@ + (void) setReplacementDictionary:(NSDictionary *)aReplacementDictionary } } ++ (NSDictionary *) offsetDictionary +{ + return offsetDictionary; +} + ++ (void) setOffsetDictionary:(NSDictionary *)anOffsetDictionary +{ + if (anOffsetDictionary == offsetDictionary) + return; + + for (id key in [anOffsetDictionary allKeys]) + { + if (![key isKindOfClass:[NSString class]]) + { + NSLog(@"ERROR: Offset key must be a string."); + return; + } + + id value = [anOffsetDictionary valueForKey:key]; + if (![value isKindOfClass:[NSNumber class]]) + { + NSLog(@"ERROR: Offsetvalue must be a number."); + return; + } + } + + [offsetDictionary release]; + offsetDictionary = [anOffsetDictionary retain]; +} + @end From d2171994e70b0d857d4526c83da09fa60b3ad570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Sat, 14 Jan 2012 10:52:45 +0100 Subject: [PATCH 05/13] Fixed offest adjustment text drawing --- FontReplacer Demo/AdjustmentViewController.h | 21 +++--- FontReplacer Demo/AdjustmentViewController.m | 70 ++++++++++++------- .../AdjustmentViewController.xib | 4 +- 3 files changed, 59 insertions(+), 36 deletions(-) diff --git a/FontReplacer Demo/AdjustmentViewController.h b/FontReplacer Demo/AdjustmentViewController.h index afd5163..a29d20f 100644 --- a/FontReplacer Demo/AdjustmentViewController.h +++ b/FontReplacer Demo/AdjustmentViewController.h @@ -13,16 +13,17 @@ @interface AdjustmentViewController : UIViewController { @private - UILabel *m_font1NormalLabel; - UILabel *m_font2NormalLabel; - UILabel *m_font1ItalicLabel; - UILabel *m_font2ItalicLabel; - UILabel *m_font1BoldLabel; - UILabel *m_font2BoldLabel; - UILabel *m_font1BoldItalicLabel; - UILabel *m_font2BoldItalicLabel; - OBSlider *m_factorSlider; - UISlider *m_pointSizeSlider; + UILabel *_font1NormalLabel; + UILabel *_font2NormalLabel; + UILabel *_font1ItalicLabel; + UILabel *_font2ItalicLabel; + UILabel *_font1BoldLabel; + UILabel *_font2BoldLabel; + UILabel *_font1BoldItalicLabel; + UILabel *_font2BoldItalicLabel; + OBSlider *_factorSlider; + UISlider *_pointSizeSlider; + NSDictionary *_originalReplacementDictionary; } @property (nonatomic, retain) IBOutlet UILabel *font1NormalLabel; diff --git a/FontReplacer Demo/AdjustmentViewController.m b/FontReplacer Demo/AdjustmentViewController.m index 206ae92..cc99a08 100644 --- a/FontReplacer Demo/AdjustmentViewController.m +++ b/FontReplacer Demo/AdjustmentViewController.m @@ -14,23 +14,28 @@ @interface AdjustmentViewController () - (void) releaseViews; -- (void) reloadData; +- (void) reloadReplacedFontLabels; +- (void) reloadReplacementFontLabels; + +@property (nonatomic, retain) NSDictionary *originalReplacementDictionary; @end @implementation AdjustmentViewController -@synthesize font1NormalLabel = m_font1NormalLabel; -@synthesize font2NormalLabel = m_font2NormalLabel; -@synthesize font1ItalicLabel = m_font1ItalicLabel; -@synthesize font2ItalicLabel = m_font2ItalicLabel; -@synthesize font1BoldLabel = m_font1BoldLabel; -@synthesize font2BoldLabel = m_font2BoldLabel; -@synthesize font1BoldItalicLabel = m_font1BoldItalicLabel; -@synthesize font2BoldItalicLabel = m_font2BoldItalicLabel; +@synthesize font1NormalLabel = _font1NormalLabel; +@synthesize font2NormalLabel = _font2NormalLabel; +@synthesize font1ItalicLabel = _font1ItalicLabel; +@synthesize font2ItalicLabel = _font2ItalicLabel; +@synthesize font1BoldLabel = _font1BoldLabel; +@synthesize font2BoldLabel = _font2BoldLabel; +@synthesize font1BoldItalicLabel = _font1BoldItalicLabel; +@synthesize font2BoldItalicLabel = _font2BoldItalicLabel; + +@synthesize offsetSlider = _factorSlider; +@synthesize pointSizeSlider = _pointSizeSlider; -@synthesize offsetSlider = m_factorSlider; -@synthesize pointSizeSlider = m_pointSizeSlider; +@synthesize originalReplacementDictionary = _originalReplacementDictionary; // MARK: - Object creation and destruction @@ -45,6 +50,7 @@ - (id) init - (void) dealloc { [self releaseViews]; + self.originalReplacementDictionary = nil; [super dealloc]; } @@ -59,7 +65,7 @@ - (void) releaseViews self.font1BoldItalicLabel = nil; self.font2BoldItalicLabel = nil; self.offsetSlider = nil; - self.pointSizeSlider = nil; + self.pointSizeSlider = nil; } // MARK: - View lifecycle @@ -67,10 +73,11 @@ - (void) releaseViews - (void) viewDidLoad { [super viewDidLoad]; - + + self.originalReplacementDictionary = [UIFont replacementDictionary]; self.pointSizeSlider.value = self.font1NormalLabel.font.pointSize; - [self reloadData]; + [self reloadReplacedFontLabels]; } - (void) viewDidUnload @@ -81,22 +88,26 @@ - (void) viewDidUnload // MARK: - Reloading screen -- (void) reloadData +- (void) reloadReplacedFontLabels { + [UIFont setReplacementDictionary:nil]; + + self.font1NormalLabel.font = [UIFont fontWithName:@"ArialMT" size:floorf(self.pointSizeSlider.value)]; + self.font1ItalicLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:floorf(self.pointSizeSlider.value)]; + self.font1BoldLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:floorf(self.pointSizeSlider.value)]; + self.font1BoldItalicLabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:floorf(self.pointSizeSlider.value)]; +} + +- (void) reloadReplacementFontLabels +{ + [UIFont setReplacementDictionary:self.originalReplacementDictionary]; + NSMutableDictionary *offsetDictionary = [NSMutableDictionary dictionary]; [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"ArialMT"]; [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"Arial-ItalicMT"]; [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"Arial-BoldMT"]; [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"Arial-BoldItalicMT"]; [UIFont setOffsetDictionary:[NSDictionary dictionaryWithDictionary:offsetDictionary]]; - - NSDictionary *originalReplacementDictionary = [UIFont replacementDictionary]; - [UIFont setReplacementDictionary:nil]; - self.font1NormalLabel.font = [UIFont fontWithName:@"ArialMT" size:floorf(self.pointSizeSlider.value)]; - self.font1ItalicLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:floorf(self.pointSizeSlider.value)]; - self.font1BoldLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:floorf(self.pointSizeSlider.value)]; - self.font1BoldItalicLabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:floorf(self.pointSizeSlider.value)]; - [UIFont setReplacementDictionary:originalReplacementDictionary]; self.font2NormalLabel.font = [UIFont fontWithName:@"ArialMT" size:floorf(self.pointSizeSlider.value)]; self.font2ItalicLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:floorf(self.pointSizeSlider.value)]; @@ -114,7 +125,18 @@ - (void) reloadData - (IBAction) settingsChanged:(id)sender { - [self reloadData]; + // Reload the screen. This is made in two steps separated by a run loop. The reason for this trick (only needed + // for this special screen where we need both fonts to be displayed) is that drawing of the labels is made: + // - once without replacement dictionary (original replaced font) + // - once with replacement dictionary (replacement font) + // If we did all this within the same method (not separated by a run loop), drawing would not be correct since + // it doest not occur when we alter the font (it occurs when the display is refreshed afterwards). If both + // the following methods were called sequentially within the same run loop, the fonts would be correct but + // drawing would occur with the configuration of the last method which has been called (here with the dictionary + // installed). This would yield incorrect metrics for drawing the labels using the first font, leading to + // incorrect results (truncated labels in my tests) + [self reloadReplacedFontLabels]; + [self performSelector:@selector(reloadReplacementFontLabels) withObject:self afterDelay:0.]; } @end diff --git a/FontReplacer Demo/AdjustmentViewController.xib b/FontReplacer Demo/AdjustmentViewController.xib index 4098548..5ec2efe 100644 --- a/FontReplacer Demo/AdjustmentViewController.xib +++ b/FontReplacer Demo/AdjustmentViewController.xib @@ -276,8 +276,8 @@ IBCocoaTouchFramework 0 0 - 33 - 6 + 37 + 14 60 From 83bf3f90009b5403dd47c92226c4df834fc1b255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Sat, 14 Jan 2012 11:53:13 +0100 Subject: [PATCH 06/13] Merged replacement font name and offset dictionaries into a single one --- FontReplacer Demo/AdjustmentViewController.h | 1 - FontReplacer Demo/AdjustmentViewController.m | 28 ++--- FontReplacer Demo/AppDelegate.m | 2 +- .../FontReplacer Demo-Info.plist | 22 +++- UIFont+Replacement/UIFont+Replacement.h | 3 - UIFont+Replacement/UIFont+Replacement.m | 106 +++++++++--------- 6 files changed, 84 insertions(+), 78 deletions(-) diff --git a/FontReplacer Demo/AdjustmentViewController.h b/FontReplacer Demo/AdjustmentViewController.h index a29d20f..b7f863f 100644 --- a/FontReplacer Demo/AdjustmentViewController.h +++ b/FontReplacer Demo/AdjustmentViewController.h @@ -23,7 +23,6 @@ UILabel *_font2BoldItalicLabel; OBSlider *_factorSlider; UISlider *_pointSizeSlider; - NSDictionary *_originalReplacementDictionary; } @property (nonatomic, retain) IBOutlet UILabel *font1NormalLabel; diff --git a/FontReplacer Demo/AdjustmentViewController.m b/FontReplacer Demo/AdjustmentViewController.m index cc99a08..3623272 100644 --- a/FontReplacer Demo/AdjustmentViewController.m +++ b/FontReplacer Demo/AdjustmentViewController.m @@ -17,8 +17,6 @@ - (void) releaseViews; - (void) reloadReplacedFontLabels; - (void) reloadReplacementFontLabels; -@property (nonatomic, retain) NSDictionary *originalReplacementDictionary; - @end @implementation AdjustmentViewController @@ -35,8 +33,6 @@ @implementation AdjustmentViewController @synthesize offsetSlider = _factorSlider; @synthesize pointSizeSlider = _pointSizeSlider; -@synthesize originalReplacementDictionary = _originalReplacementDictionary; - // MARK: - Object creation and destruction - (id) init @@ -50,7 +46,6 @@ - (id) init - (void) dealloc { [self releaseViews]; - self.originalReplacementDictionary = nil; [super dealloc]; } @@ -74,7 +69,6 @@ - (void) viewDidLoad { [super viewDidLoad]; - self.originalReplacementDictionary = [UIFont replacementDictionary]; self.pointSizeSlider.value = self.font1NormalLabel.font.pointSize; [self reloadReplacedFontLabels]; @@ -100,14 +94,20 @@ - (void) reloadReplacedFontLabels - (void) reloadReplacementFontLabels { - [UIFont setReplacementDictionary:self.originalReplacementDictionary]; - - NSMutableDictionary *offsetDictionary = [NSMutableDictionary dictionary]; - [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"ArialMT"]; - [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"Arial-ItalicMT"]; - [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"Arial-BoldMT"]; - [offsetDictionary setObject:[NSNumber numberWithFloat:self.offsetSlider.value] forKey:@"Arial-BoldItalicMT"]; - [UIFont setOffsetDictionary:[NSDictionary dictionaryWithDictionary:offsetDictionary]]; + NSMutableDictionary *replacementDictionary = [NSMutableDictionary dictionary]; + [replacementDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"CaviarDreams", @"Name", + [NSNumber numberWithFloat:self.offsetSlider.value], @"Offset", nil] + forKey:@"ArialMT"]; + [replacementDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"CaviarDreams-Italic", @"Name", + [NSNumber numberWithFloat:self.offsetSlider.value], @"Offset", nil] + forKey:@"Arial-ItalicMT"]; + [replacementDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"CaviarDreams-Bold", @"Name", + [NSNumber numberWithFloat:self.offsetSlider.value], @"Offset", nil] + forKey:@"Arial-BoldMT"]; + [replacementDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"CaviarDreams-BoldItalic", @"Name", + [NSNumber numberWithFloat:self.offsetSlider.value], @"Offset", nil] + forKey:@"Arial-BoldItalicMT"]; + [UIFont setReplacementDictionary:replacementDictionary]; self.font2NormalLabel.font = [UIFont fontWithName:@"ArialMT" size:floorf(self.pointSizeSlider.value)]; self.font2ItalicLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:floorf(self.pointSizeSlider.value)]; diff --git a/FontReplacer Demo/AppDelegate.m b/FontReplacer Demo/AppDelegate.m index d10923e..178ff34 100644 --- a/FontReplacer Demo/AppDelegate.m +++ b/FontReplacer Demo/AppDelegate.m @@ -39,7 +39,7 @@ - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions: - (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { - if ([viewController isKindOfClass:[FontsViewController class]]) + if ([viewController isKindOfClass:[UINavigationController class]]) { self.originalReplacementDictionary = [UIFont replacementDictionary]; [UIFont setReplacementDictionary:nil]; diff --git a/FontReplacer Demo/FontReplacer Demo-Info.plist b/FontReplacer Demo/FontReplacer Demo-Info.plist index 928160e..4f4116a 100644 --- a/FontReplacer Demo/FontReplacer Demo-Info.plist +++ b/FontReplacer Demo/FontReplacer Demo-Info.plist @@ -29,13 +29,27 @@ ReplacementFonts ArialMT - CaviarDreams + + Name + CaviarDreams + Arial-ItalicMT - CaviarDreams-Italic + + Name + CaviarDreams-Italic + Offset + 0.3 + Arial-BoldMT - CaviarDreams-Bold + + Name + CaviarDreams-Bold + Arial-BoldItalicMT - CaviarDreams-BoldItalic + + Name + CaviarDreams-BoldItalic + UIAppFonts diff --git a/UIFont+Replacement/UIFont+Replacement.h b/UIFont+Replacement/UIFont+Replacement.h index 48aaa60..8511d4e 100644 --- a/UIFont+Replacement/UIFont+Replacement.h +++ b/UIFont+Replacement/UIFont+Replacement.h @@ -13,7 +13,4 @@ + (NSDictionary *) replacementDictionary; + (void) setReplacementDictionary:(NSDictionary *)aReplacementDictionary; -+ (NSDictionary *) offsetDictionary; -+ (void) setOffsetDictionary:(NSDictionary *)anOffsetDictionary; - @end diff --git a/UIFont+Replacement/UIFont+Replacement.m b/UIFont+Replacement/UIFont+Replacement.m index 8e55ae8..59c7e62 100644 --- a/UIFont+Replacement/UIFont+Replacement.m +++ b/UIFont+Replacement/UIFont+Replacement.m @@ -12,7 +12,7 @@ @implementation UIFont (Replacement) static NSDictionary *replacementDictionary = nil; -static NSDictionary *offsetDictionary = nil; +static NSDictionary *inverseReplacementDictionary = nil; static void initializeReplacementFonts() { @@ -23,9 +23,6 @@ static void initializeReplacementFonts() NSDictionary *replacementDictionary = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"ReplacementFonts"]; [UIFont setReplacementDictionary:replacementDictionary]; - - NSDictionary *offsetDictionary = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"ReplacementOffsets"]; - [UIFont setOffsetDictionary:offsetDictionary]; } + (void) load @@ -46,28 +43,34 @@ + (void) load method_exchangeImplementations(ascender_, replacementAscender_); } -+ (CGFloat) offsetForFontWithName:(NSString *)fontName ++ (NSDictionary *) replacementInfoForFontWithName:(NSString *)fontName { - if (!offsetDictionary) - { - return 0.f; - } + if (!replacementDictionary) + return nil; - return [[offsetDictionary objectForKey:fontName] floatValue]; + return [replacementDictionary objectForKey:fontName]; +} + ++ (NSString *) replacementFontNameForFontWithName:(NSString *)fontName +{ + return [[self replacementInfoForFontWithName:fontName] objectForKey:@"Name"] ?: fontName; +} + ++ (CGFloat) offsetForFontWithName:(NSString *)fontName +{ + return [[[self replacementInfoForFontWithName:fontName] objectForKey:@"Offset"] floatValue]; } + (UIFont *) replacement_fontWithName:(NSString *)fontName size:(CGFloat)fontSize { initializeReplacementFonts(); - NSString *replacementFontName = [replacementDictionary objectForKey:fontName]; - return [self replacement_fontWithName:replacementFontName ?: fontName size:fontSize]; + return [self replacement_fontWithName:[self replacementFontNameForFontWithName:fontName] size:fontSize]; } + (UIFont *) replacement_fontWithName:(NSString *)fontName size:(CGFloat)fontSize traits:(int)traits { initializeReplacementFonts(); - NSString *replacementFontName = [replacementDictionary objectForKey:fontName]; - return [self replacement_fontWithName:replacementFontName ?: fontName size:fontSize traits:traits]; + return [self replacement_fontWithName:[self replacementFontNameForFontWithName:fontName] size:fontSize traits:traits]; } - (CGFloat) replacement_ascender @@ -75,20 +78,23 @@ - (CGFloat) replacement_ascender NSString *fontName = [self fontName]; CGFloat ascender = [self replacement_ascender]; - NSArray *replacedFontNames = [replacementDictionary allKeysForObject:fontName]; - if ([replacedFontNames count] == 0) + // The receiver is not replacing any font. Return original ascender value + NSString *replacedFontName = [inverseReplacementDictionary objectForKey:fontName]; + if (!replacedFontName) { return ascender; } - - NSString *replacedFontName = [replacedFontNames objectAtIndex:0]; - // Trick: To get to the original font, remove the replacement dictionary temporarily - NSDictionary *originalReplacementDictionary = [UIFont replacementDictionary]; + // The receiver is replacing another font: To access the replaced font, we have to remove the replacement dictionary + // temporarily + NSDictionary *originalReplacementDictionary = [[[UIFont replacementDictionary] retain] autorelease]; [UIFont setReplacementDictionary:nil]; UIFont *replacedFont = [UIFont fontWithName:replacedFontName size:self.pointSize]; [UIFont setReplacementDictionary:originalReplacementDictionary]; + // Adjust the receiver ascender value. A good default behavior is to match the ascender value of the replacing + // font to match the one of the replaced font. If this default behavior is not convincing enough, an offset + // can be provided return [replacedFont replacement_ascender] + self.pointSize * [UIFont offsetForFontWithName:replacedFontName]; } @@ -102,6 +108,7 @@ + (void) setReplacementDictionary:(NSDictionary *)aReplacementDictionary if (aReplacementDictionary == replacementDictionary) return; + NSMutableDictionary *anInverseReplacementDictionary = [NSMutableDictionary dictionary]; for (id key in [aReplacementDictionary allKeys]) { if (![key isKindOfClass:[NSString class]]) @@ -110,54 +117,43 @@ + (void) setReplacementDictionary:(NSDictionary *)aReplacementDictionary return; } - id value = [aReplacementDictionary valueForKey:key]; - if (![value isKindOfClass:[NSString class]]) + NSString *fontName = (NSString *)key; + id value = [aReplacementDictionary valueForKey:fontName]; + if (![value isKindOfClass:[NSDictionary class]]) { - NSLog(@"ERROR: Replacement font value must be a string."); + NSLog(@"ERROR: Replacement font value must be a dictionary."); return; } - } - - [replacementDictionary release]; - replacementDictionary = [aReplacementDictionary retain]; - - for (id key in [replacementDictionary allKeys]) - { - NSString *fontName = [replacementDictionary objectForKey:key]; - UIFont *font = [UIFont fontWithName:fontName size:10]; + + NSDictionary *replacementInfo = (NSDictionary *)value; + NSString *replacementFontName = [replacementInfo objectForKey:@"Name"]; + if (!replacementFontName) + { + NSLog(@"ERROR: Missing replacement font name for font '%@'", fontName); + return; + } + + UIFont *font = [UIFont fontWithName:replacementFontName size:10.f]; if (!font) - NSLog(@"WARNING: replacement font '%@' is not available.", fontName); - } -} - -+ (NSDictionary *) offsetDictionary -{ - return offsetDictionary; -} - -+ (void) setOffsetDictionary:(NSDictionary *)anOffsetDictionary -{ - if (anOffsetDictionary == offsetDictionary) - return; - - for (id key in [anOffsetDictionary allKeys]) - { - if (![key isKindOfClass:[NSString class]]) { - NSLog(@"ERROR: Offset key must be a string."); + NSLog(@"ERROR: The replacement font '%@' is not available", replacementFontName); return; } - id value = [anOffsetDictionary valueForKey:key]; - if (![value isKindOfClass:[NSNumber class]]) + if ([anInverseReplacementDictionary objectForKey:replacementFontName]) { - NSLog(@"ERROR: Offsetvalue must be a number."); + NSLog(@"ERROR: A font can replace at most one other font. This is not the case for font '%@'", replacementFontName); return; } + + [anInverseReplacementDictionary setObject:fontName forKey:replacementFontName]; } - [offsetDictionary release]; - offsetDictionary = [anOffsetDictionary retain]; + [replacementDictionary release]; + replacementDictionary = [aReplacementDictionary retain]; + + [inverseReplacementDictionary release]; + inverseReplacementDictionary = [anInverseReplacementDictionary retain]; } @end From 68dab5cd3bc852022ebf32fe13fd8f147c7b7ff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Sat, 14 Jan 2012 12:05:35 +0100 Subject: [PATCH 07/13] Using plist dictionary for the demo --- FontReplacer Demo/AppDelegate.h | 2 -- FontReplacer Demo/AppDelegate.m | 10 ++-------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/FontReplacer Demo/AppDelegate.h b/FontReplacer Demo/AppDelegate.h index 1b85cab..d378a25 100644 --- a/FontReplacer Demo/AppDelegate.h +++ b/FontReplacer Demo/AppDelegate.h @@ -8,6 +8,4 @@ @interface AppDelegate : UIResponder -@property (nonatomic, retain) NSDictionary *originalReplacementDictionary; - @end diff --git a/FontReplacer Demo/AppDelegate.m b/FontReplacer Demo/AppDelegate.m index 178ff34..508a7a9 100644 --- a/FontReplacer Demo/AppDelegate.m +++ b/FontReplacer Demo/AppDelegate.m @@ -14,8 +14,6 @@ @implementation AppDelegate -@synthesize originalReplacementDictionary; - - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; @@ -41,16 +39,12 @@ - (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectVie { if ([viewController isKindOfClass:[UINavigationController class]]) { - self.originalReplacementDictionary = [UIFont replacementDictionary]; [UIFont setReplacementDictionary:nil]; } else { - if (self.originalReplacementDictionary) - { - [UIFont setReplacementDictionary:self.originalReplacementDictionary]; - self.originalReplacementDictionary = nil; - } + NSDictionary *plistDictionary = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"ReplacementFonts"]; + [UIFont setReplacementDictionary:plistDictionary]; } return YES; From 1c96ff84b88b6aac4ecfd46e2a69c0d44109488e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Sun, 15 Jan 2012 16:16:25 +0100 Subject: [PATCH 08/13] Only one font adjusted at a time --- FontReplacer Demo/AdjustmentViewController.h | 33 +++----- FontReplacer Demo/AdjustmentViewController.m | 71 +++++++++------- .../AdjustmentViewController.xib | 83 ++++++++----------- 3 files changed, 87 insertions(+), 100 deletions(-) diff --git a/FontReplacer Demo/AdjustmentViewController.h b/FontReplacer Demo/AdjustmentViewController.h index b7f863f..7debc0b 100644 --- a/FontReplacer Demo/AdjustmentViewController.h +++ b/FontReplacer Demo/AdjustmentViewController.h @@ -11,28 +11,17 @@ #import "OBSlider.h" @interface AdjustmentViewController : UIViewController -{ -@private - UILabel *_font1NormalLabel; - UILabel *_font2NormalLabel; - UILabel *_font1ItalicLabel; - UILabel *_font2ItalicLabel; - UILabel *_font1BoldLabel; - UILabel *_font2BoldLabel; - UILabel *_font1BoldItalicLabel; - UILabel *_font2BoldItalicLabel; - OBSlider *_factorSlider; - UISlider *_pointSizeSlider; -} - -@property (nonatomic, retain) IBOutlet UILabel *font1NormalLabel; -@property (nonatomic, retain) IBOutlet UILabel *font2NormalLabel; -@property (nonatomic, retain) IBOutlet UILabel *font1ItalicLabel; -@property (nonatomic, retain) IBOutlet UILabel *font2ItalicLabel; -@property (nonatomic, retain) IBOutlet UILabel *font1BoldLabel; -@property (nonatomic, retain) IBOutlet UILabel *font2BoldLabel; -@property (nonatomic, retain) IBOutlet UILabel *font1BoldItalicLabel; -@property (nonatomic, retain) IBOutlet UILabel *font2BoldItalicLabel; + +- (id) initWithFontName:(NSString *)fontName; + +@property (nonatomic, retain) IBOutlet UILabel *font1FirstLabel; +@property (nonatomic, retain) IBOutlet UILabel *font2FirstLabel; +@property (nonatomic, retain) IBOutlet UILabel *font1SecondLabel; +@property (nonatomic, retain) IBOutlet UILabel *font2SecondLabel; +@property (nonatomic, retain) IBOutlet UILabel *font1ThirdLabel; +@property (nonatomic, retain) IBOutlet UILabel *font2ThirdLabel; +@property (nonatomic, retain) IBOutlet UILabel *font1FourthLabel; +@property (nonatomic, retain) IBOutlet UILabel *font2FourthLabel; @property (nonatomic, retain) IBOutlet OBSlider *offsetSlider; @property (nonatomic, retain) IBOutlet UISlider *pointSizeSlider; diff --git a/FontReplacer Demo/AdjustmentViewController.m b/FontReplacer Demo/AdjustmentViewController.m index 3623272..6fe4dab 100644 --- a/FontReplacer Demo/AdjustmentViewController.m +++ b/FontReplacer Demo/AdjustmentViewController.m @@ -12,6 +12,8 @@ @interface AdjustmentViewController () +@property (nonatomic, retain) NSString *fontName; + - (void) releaseViews; - (void) reloadReplacedFontLabels; @@ -21,44 +23,53 @@ - (void) reloadReplacementFontLabels; @implementation AdjustmentViewController -@synthesize font1NormalLabel = _font1NormalLabel; -@synthesize font2NormalLabel = _font2NormalLabel; -@synthesize font1ItalicLabel = _font1ItalicLabel; -@synthesize font2ItalicLabel = _font2ItalicLabel; -@synthesize font1BoldLabel = _font1BoldLabel; -@synthesize font2BoldLabel = _font2BoldLabel; -@synthesize font1BoldItalicLabel = _font1BoldItalicLabel; -@synthesize font2BoldItalicLabel = _font2BoldItalicLabel; +@synthesize fontName = _fontName; + +@synthesize font1FirstLabel = _font1FirstLabel; +@synthesize font2FirstLabel = _font2FirstLabel; +@synthesize font1SecondLabel = _font1SecondLabel; +@synthesize font2SecondLabel = _font2SecondLabel; +@synthesize font1ThirdLabel = _font1ThirdLabel; +@synthesize font2ThirdLabel = _font2ThirdLabel; +@synthesize font1FourthLabel = _font1FourthLabel; +@synthesize font2FourthLabel = _font2FourthLabel; @synthesize offsetSlider = _factorSlider; @synthesize pointSizeSlider = _pointSizeSlider; // MARK: - Object creation and destruction -- (id) init +- (id) initWithFontName:(NSString *)fontName { if (!(self = [super initWithNibName:@"AdjustmentViewController" bundle:nil])) return nil; + self.fontName = fontName; return self; } +- (id) init +{ + return [self initWithFontName:[[UIFont systemFontOfSize:10.f] fontName]]; +} + - (void) dealloc { [self releaseViews]; + self.fontName = nil; [super dealloc]; } - (void) releaseViews { - self.font1NormalLabel = nil; - self.font2NormalLabel = nil; - self.font1ItalicLabel = nil; - self.font2ItalicLabel = nil; - self.font1BoldLabel = nil; - self.font2BoldLabel = nil; - self.font1BoldItalicLabel = nil; - self.font2BoldItalicLabel = nil; + self.font1FirstLabel = nil; + self.font2FirstLabel = nil; + self.font1SecondLabel = nil; + self.font2SecondLabel = nil; + self.font1ThirdLabel = nil; + self.font2ThirdLabel = nil; + self.font1FourthLabel = nil; + self.font2FourthLabel = nil; self.offsetSlider = nil; self.pointSizeSlider = nil; } @@ -69,7 +80,7 @@ - (void) viewDidLoad { [super viewDidLoad]; - self.pointSizeSlider.value = self.font1NormalLabel.font.pointSize; + self.pointSizeSlider.value = self.font1FirstLabel.font.pointSize; [self reloadReplacedFontLabels]; } @@ -86,10 +97,10 @@ - (void) reloadReplacedFontLabels { [UIFont setReplacementDictionary:nil]; - self.font1NormalLabel.font = [UIFont fontWithName:@"ArialMT" size:floorf(self.pointSizeSlider.value)]; - self.font1ItalicLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:floorf(self.pointSizeSlider.value)]; - self.font1BoldLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:floorf(self.pointSizeSlider.value)]; - self.font1BoldItalicLabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:floorf(self.pointSizeSlider.value)]; + self.font1FirstLabel.font = [UIFont fontWithName:self.fontName size:floorf(self.pointSizeSlider.value)]; + self.font1SecondLabel.font = [UIFont fontWithName:self.fontName size:floorf(self.pointSizeSlider.value)]; + self.font1ThirdLabel.font = [UIFont fontWithName:self.fontName size:floorf(self.pointSizeSlider.value)]; + self.font1FourthLabel.font = [UIFont fontWithName:self.fontName size:floorf(self.pointSizeSlider.value)]; } - (void) reloadReplacementFontLabels @@ -109,16 +120,16 @@ - (void) reloadReplacementFontLabels forKey:@"Arial-BoldItalicMT"]; [UIFont setReplacementDictionary:replacementDictionary]; - self.font2NormalLabel.font = [UIFont fontWithName:@"ArialMT" size:floorf(self.pointSizeSlider.value)]; - self.font2ItalicLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:floorf(self.pointSizeSlider.value)]; - self.font2BoldLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:floorf(self.pointSizeSlider.value)]; - self.font2BoldItalicLabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:floorf(self.pointSizeSlider.value)]; + self.font2FirstLabel.font = [UIFont fontWithName:@"ArialMT" size:floorf(self.pointSizeSlider.value)]; + self.font2SecondLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:floorf(self.pointSizeSlider.value)]; + self.font2ThirdLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:floorf(self.pointSizeSlider.value)]; + self.font2FourthLabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:floorf(self.pointSizeSlider.value)]; // Force a refresh - [self.font2NormalLabel setNeedsDisplay]; - [self.font2ItalicLabel setNeedsDisplay]; - [self.font2BoldLabel setNeedsDisplay]; - [self.font2BoldItalicLabel setNeedsDisplay]; + [self.font2FirstLabel setNeedsDisplay]; + [self.font2SecondLabel setNeedsDisplay]; + [self.font2ThirdLabel setNeedsDisplay]; + [self.font2FourthLabel setNeedsDisplay]; } // MARK: - Event callbacks diff --git a/FontReplacer Demo/AdjustmentViewController.xib b/FontReplacer Demo/AdjustmentViewController.xib index 5ec2efe..2927885 100644 --- a/FontReplacer Demo/AdjustmentViewController.xib +++ b/FontReplacer Demo/AdjustmentViewController.xib @@ -33,7 +33,7 @@ IBCocoaTouchFramework - + 274 @@ -41,7 +41,6 @@ 292 {{20, 8}, {280, 70}} - NO YES @@ -75,7 +74,6 @@ 292 {{20, 8}, {280, 70}} - NO YES @@ -99,7 +97,6 @@ 292 {{20, 86}, {280, 70}} - NO YES @@ -107,7 +104,7 @@ 7 NO IBCocoaTouchFramework - Sphinx + befgix 1 @@ -130,14 +127,13 @@ 292 {{20, 86}, {280, 70}} - NO YES 7 NO IBCocoaTouchFramework - Sphinx + befgix 1 @@ -151,7 +147,6 @@ 292 {{20, 164}, {280, 70}} - NO YES @@ -159,7 +154,7 @@ 7 NO IBCocoaTouchFramework - Sphinx + ACIQSW 1 @@ -182,14 +177,13 @@ 292 {{20, 164}, {280, 70}} - NO YES 7 NO IBCocoaTouchFramework - Sphinx + ACIQSW 1 @@ -203,7 +197,6 @@ 292 {{20, 242}, {280, 70}} - NO YES @@ -211,7 +204,7 @@ 7 NO IBCocoaTouchFramework - Sphinx + 134580 1 @@ -234,14 +227,13 @@ 292 {{20, 242}, {280, 70}} - NO YES 7 NO IBCocoaTouchFramework - Sphinx + 134580 1 @@ -255,7 +247,6 @@ 292 {{18, 320}, {278, 23}} - _NS:623 NO @@ -269,8 +260,6 @@ 292 {{18, 369}, {278, 23}} - - _NS:623 NO IBCocoaTouchFramework @@ -282,8 +271,6 @@ {{0, 20}, {320, 411}} - - 3 @@ -318,75 +305,75 @@ - font1ItalicLabel + offsetSlider - + - 35 + 41 - font2ItalicLabel + font1FirstLabel - + - 36 + 46 - font1BoldLabel + font2FirstLabel - + - 37 + 47 - font2BoldLabel + font1SecondLabel - + - 38 + 48 - font1BoldItalicLabel + font2SecondLabel - + - 39 + 49 - font2BoldItalicLabel + font1ThirdLabel - + - 40 + 50 - offsetSlider + font2ThirdLabel - + - 41 + 51 - font2NormalLabel + font1FourthLabel - + - 34 + 52 - font1NormalLabel + font2FourthLabel - + - 33 + 53 @@ -517,7 +504,7 @@ - 45 + 53 From dddcb526e8d752bc227b9babafc4defd607b1c42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Sun, 15 Jan 2012 16:26:23 +0100 Subject: [PATCH 09/13] Adjusting fonts individually --- FontReplacer Demo.xcodeproj/project.pbxproj | 12 - FontReplacer Demo/AdjustmentViewController.h | 5 +- FontReplacer Demo/AdjustmentViewController.m | 89 ++++--- .../AdjustmentViewController.xib | 248 +++++++++++++++--- FontReplacer Demo/AppDelegate.m | 6 +- FontReplacer Demo/ComparatorViewController.h | 16 -- FontReplacer Demo/ComparatorViewController.m | 86 ------ FontReplacer Demo/ComparisonResult.h | 19 -- FontReplacer Demo/ComparisonResult.m | 42 --- FontReplacer Demo/FontsViewController.h | 2 + FontReplacer Demo/FontsViewController.m | 54 +++- 11 files changed, 314 insertions(+), 265 deletions(-) delete mode 100644 FontReplacer Demo/ComparatorViewController.h delete mode 100644 FontReplacer Demo/ComparatorViewController.m delete mode 100644 FontReplacer Demo/ComparisonResult.h delete mode 100644 FontReplacer Demo/ComparisonResult.m diff --git a/FontReplacer Demo.xcodeproj/project.pbxproj b/FontReplacer Demo.xcodeproj/project.pbxproj index d34267b..f69b126 100644 --- a/FontReplacer Demo.xcodeproj/project.pbxproj +++ b/FontReplacer Demo.xcodeproj/project.pbxproj @@ -12,7 +12,6 @@ 6FB0366414C0030200626500 /* OBSlider.m in Sources */ = {isa = PBXBuildFile; fileRef = 6FB0366314C0030200626500 /* OBSlider.m */; }; DA336B6B13F01F5D00A60EA6 /* DemoViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = DA336B6A13F01F5D00A60EA6 /* DemoViewController.xib */; }; DA4C182113F446120015A600 /* FontsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DA4C181F13F446110015A600 /* FontsViewController.m */; }; - DA4C182813F4506A0015A600 /* ComparatorViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DA4C182613F450680015A600 /* ComparatorViewController.m */; }; DA6C855713F02703007060AF /* CaviarDreams.ttf in Resources */ = {isa = PBXBuildFile; fileRef = DA6C855213F02703007060AF /* CaviarDreams.ttf */; }; DA6C855813F02703007060AF /* CaviarDreams_Bold.ttf in Resources */ = {isa = PBXBuildFile; fileRef = DA6C855313F02703007060AF /* CaviarDreams_Bold.ttf */; }; DA6C855913F02703007060AF /* CaviarDreams_BoldItalic.ttf in Resources */ = {isa = PBXBuildFile; fileRef = DA6C855413F02703007060AF /* CaviarDreams_BoldItalic.ttf */; }; @@ -23,7 +22,6 @@ DA98620913F01BCE006DEC9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = DA98620813F01BCE006DEC9A /* main.m */; }; DA98620D13F01BCE006DEC9A /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = DA98620C13F01BCE006DEC9A /* AppDelegate.m */; }; DA98621C13F01E63006DEC9A /* UIFont+Replacement.m in Sources */ = {isa = PBXBuildFile; fileRef = DA98621B13F01E63006DEC9A /* UIFont+Replacement.m */; }; - DADAB65A13F5DDB0000301E6 /* ComparisonResult.m in Sources */ = {isa = PBXBuildFile; fileRef = DADAB65913F5DDB0000301E6 /* ComparisonResult.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -35,8 +33,6 @@ DA336B6A13F01F5D00A60EA6 /* DemoViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = DemoViewController.xib; sourceTree = ""; }; DA4C181E13F446110015A600 /* FontsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FontsViewController.h; sourceTree = ""; }; DA4C181F13F446110015A600 /* FontsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FontsViewController.m; sourceTree = ""; }; - DA4C182513F450680015A600 /* ComparatorViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ComparatorViewController.h; sourceTree = ""; }; - DA4C182613F450680015A600 /* ComparatorViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ComparatorViewController.m; sourceTree = ""; }; DA6C855213F02703007060AF /* CaviarDreams.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = CaviarDreams.ttf; sourceTree = ""; }; DA6C855313F02703007060AF /* CaviarDreams_Bold.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = CaviarDreams_Bold.ttf; sourceTree = ""; }; DA6C855413F02703007060AF /* CaviarDreams_BoldItalic.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = CaviarDreams_BoldItalic.ttf; sourceTree = ""; }; @@ -52,8 +48,6 @@ DA98620C13F01BCE006DEC9A /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; DA98621A13F01E63006DEC9A /* UIFont+Replacement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIFont+Replacement.h"; sourceTree = ""; }; DA98621B13F01E63006DEC9A /* UIFont+Replacement.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIFont+Replacement.m"; sourceTree = ""; }; - DADAB65813F5DDAF000301E6 /* ComparisonResult.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ComparisonResult.h; sourceTree = ""; }; - DADAB65913F5DDB0000301E6 /* ComparisonResult.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ComparisonResult.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -130,10 +124,6 @@ DA336B6A13F01F5D00A60EA6 /* DemoViewController.xib */, DA4C181E13F446110015A600 /* FontsViewController.h */, DA4C181F13F446110015A600 /* FontsViewController.m */, - DA4C182513F450680015A600 /* ComparatorViewController.h */, - DA4C182613F450680015A600 /* ComparatorViewController.m */, - DADAB65813F5DDAF000301E6 /* ComparisonResult.h */, - DADAB65913F5DDB0000301E6 /* ComparisonResult.m */, DA6C855113F02703007060AF /* CaviarDreams */, DA98620313F01BCE006DEC9A /* Supporting Files */, ); @@ -232,8 +222,6 @@ DA98620D13F01BCE006DEC9A /* AppDelegate.m in Sources */, DA98621C13F01E63006DEC9A /* UIFont+Replacement.m in Sources */, DA4C182113F446120015A600 /* FontsViewController.m in Sources */, - DA4C182813F4506A0015A600 /* ComparatorViewController.m in Sources */, - DADAB65A13F5DDB0000301E6 /* ComparisonResult.m in Sources */, 6FB0365D14BFFEC900626500 /* AdjustmentViewController.m in Sources */, 6FB0366414C0030200626500 /* OBSlider.m in Sources */, ); diff --git a/FontReplacer Demo/AdjustmentViewController.h b/FontReplacer Demo/AdjustmentViewController.h index 7debc0b..20cdb8b 100644 --- a/FontReplacer Demo/AdjustmentViewController.h +++ b/FontReplacer Demo/AdjustmentViewController.h @@ -12,7 +12,7 @@ @interface AdjustmentViewController : UIViewController -- (id) initWithFontName:(NSString *)fontName; +- (id) initWithReplacedFontName:(NSString *)replacedFontName replacementFontName:(NSString *)replacementFontName; @property (nonatomic, retain) IBOutlet UILabel *font1FirstLabel; @property (nonatomic, retain) IBOutlet UILabel *font2FirstLabel; @@ -24,7 +24,10 @@ @property (nonatomic, retain) IBOutlet UILabel *font2FourthLabel; @property (nonatomic, retain) IBOutlet OBSlider *offsetSlider; +@property (nonatomic, retain) IBOutlet UILabel *offsetLabel; + @property (nonatomic, retain) IBOutlet UISlider *pointSizeSlider; +@property (nonatomic, retain) IBOutlet UILabel *pointSizeLabel; - (IBAction) settingsChanged:(id)sender; diff --git a/FontReplacer Demo/AdjustmentViewController.m b/FontReplacer Demo/AdjustmentViewController.m index 6fe4dab..08ebbd1 100644 --- a/FontReplacer Demo/AdjustmentViewController.m +++ b/FontReplacer Demo/AdjustmentViewController.m @@ -12,10 +12,12 @@ @interface AdjustmentViewController () -@property (nonatomic, retain) NSString *fontName; +@property (nonatomic, retain) NSString *replacedFontName; +@property (nonatomic, retain) NSString *replacementFontName; - (void) releaseViews; +- (void) reloadData; - (void) reloadReplacedFontLabels; - (void) reloadReplacementFontLabels; @@ -23,7 +25,8 @@ - (void) reloadReplacementFontLabels; @implementation AdjustmentViewController -@synthesize fontName = _fontName; +@synthesize replacedFontName = _replacedFontName; +@synthesize replacementFontName = _replacementFontName; @synthesize font1FirstLabel = _font1FirstLabel; @synthesize font2FirstLabel = _font2FirstLabel; @@ -35,28 +38,29 @@ @implementation AdjustmentViewController @synthesize font2FourthLabel = _font2FourthLabel; @synthesize offsetSlider = _factorSlider; +@synthesize offsetLabel = _offsetLabel; + @synthesize pointSizeSlider = _pointSizeSlider; +@synthesize pointSizeLabel = _pointSizeLabel; // MARK: - Object creation and destruction -- (id) initWithFontName:(NSString *)fontName +- (id) initWithReplacedFontName:(NSString *)replacedFontName replacementFontName:(NSString *)replacementFontName { if (!(self = [super initWithNibName:@"AdjustmentViewController" bundle:nil])) return nil; - self.fontName = fontName; + self.replacedFontName = replacedFontName; + self.replacementFontName = replacementFontName; + self.title = @"Offset adjustment"; return self; } -- (id) init -{ - return [self initWithFontName:[[UIFont systemFontOfSize:10.f] fontName]]; -} - - (void) dealloc { [self releaseViews]; - self.fontName = nil; + self.replacedFontName = nil; + self.replacementFontName = nil; [super dealloc]; } @@ -71,7 +75,9 @@ - (void) releaseViews self.font1FourthLabel = nil; self.font2FourthLabel = nil; self.offsetSlider = nil; + self.offsetLabel = nil; self.pointSizeSlider = nil; + self.pointSizeLabel = nil; } // MARK: - View lifecycle @@ -82,7 +88,7 @@ - (void) viewDidLoad self.pointSizeSlider.value = self.font1FirstLabel.font.pointSize; - [self reloadReplacedFontLabels]; + [self reloadData]; } - (void) viewDidUnload @@ -93,37 +99,47 @@ - (void) viewDidUnload // MARK: - Reloading screen +- (void) reloadData +{ + self.offsetLabel.text = [NSString stringWithFormat:@"%.3f", self.offsetSlider.value]; + self.pointSizeLabel.text = [NSString stringWithFormat:@"%d", (NSUInteger)self.pointSizeSlider.value]; + + // Reload the screen. This is made in two steps separated by a run loop. The reason for this trick (only needed + // for this special screen where we need both fonts to be displayed) is that drawing of the labels is made: + // - once without replacement dictionary (original replaced font) + // - once with replacement dictionary (replacement font) + // If we did all this within the same method (not separated by a run loop), drawing would not be correct since + // it doest not occur when we alter the font (it occurs when the display is refreshed afterwards). If both + // the following methods were called sequentially within the same run loop, the fonts would be correct but + // drawing would occur with the configuration of the last method which has been called (here with the dictionary + // installed). This would yield incorrect metrics for drawing the labels using the first font, leading to + // incorrect results (truncated labels in my tests) + [self reloadReplacedFontLabels]; + [self performSelector:@selector(reloadReplacementFontLabels) withObject:self afterDelay:0.]; +} + - (void) reloadReplacedFontLabels { [UIFont setReplacementDictionary:nil]; - self.font1FirstLabel.font = [UIFont fontWithName:self.fontName size:floorf(self.pointSizeSlider.value)]; - self.font1SecondLabel.font = [UIFont fontWithName:self.fontName size:floorf(self.pointSizeSlider.value)]; - self.font1ThirdLabel.font = [UIFont fontWithName:self.fontName size:floorf(self.pointSizeSlider.value)]; - self.font1FourthLabel.font = [UIFont fontWithName:self.fontName size:floorf(self.pointSizeSlider.value)]; + self.font1FirstLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.font1SecondLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.font1ThirdLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.font1FourthLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; } - (void) reloadReplacementFontLabels { NSMutableDictionary *replacementDictionary = [NSMutableDictionary dictionary]; - [replacementDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"CaviarDreams", @"Name", - [NSNumber numberWithFloat:self.offsetSlider.value], @"Offset", nil] - forKey:@"ArialMT"]; - [replacementDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"CaviarDreams-Italic", @"Name", - [NSNumber numberWithFloat:self.offsetSlider.value], @"Offset", nil] - forKey:@"Arial-ItalicMT"]; - [replacementDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"CaviarDreams-Bold", @"Name", + [replacementDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys:self.replacementFontName, @"Name", [NSNumber numberWithFloat:self.offsetSlider.value], @"Offset", nil] - forKey:@"Arial-BoldMT"]; - [replacementDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"CaviarDreams-BoldItalic", @"Name", - [NSNumber numberWithFloat:self.offsetSlider.value], @"Offset", nil] - forKey:@"Arial-BoldItalicMT"]; + forKey:self.replacedFontName]; [UIFont setReplacementDictionary:replacementDictionary]; - self.font2FirstLabel.font = [UIFont fontWithName:@"ArialMT" size:floorf(self.pointSizeSlider.value)]; - self.font2SecondLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:floorf(self.pointSizeSlider.value)]; - self.font2ThirdLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:floorf(self.pointSizeSlider.value)]; - self.font2FourthLabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:floorf(self.pointSizeSlider.value)]; + self.font2FirstLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.font2SecondLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.font2ThirdLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.font2FourthLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; // Force a refresh [self.font2FirstLabel setNeedsDisplay]; @@ -136,18 +152,7 @@ - (void) reloadReplacementFontLabels - (IBAction) settingsChanged:(id)sender { - // Reload the screen. This is made in two steps separated by a run loop. The reason for this trick (only needed - // for this special screen where we need both fonts to be displayed) is that drawing of the labels is made: - // - once without replacement dictionary (original replaced font) - // - once with replacement dictionary (replacement font) - // If we did all this within the same method (not separated by a run loop), drawing would not be correct since - // it doest not occur when we alter the font (it occurs when the display is refreshed afterwards). If both - // the following methods were called sequentially within the same run loop, the fonts would be correct but - // drawing would occur with the configuration of the last method which has been called (here with the dictionary - // installed). This would yield incorrect metrics for drawing the labels using the first font, leading to - // incorrect results (truncated labels in my tests) - [self reloadReplacedFontLabels]; - [self performSelector:@selector(reloadReplacementFontLabels) withObject:self afterDelay:0.]; + [self reloadData]; } @end diff --git a/FontReplacer Demo/AdjustmentViewController.xib b/FontReplacer Demo/AdjustmentViewController.xib index 2927885..93e09e7 100644 --- a/FontReplacer Demo/AdjustmentViewController.xib +++ b/FontReplacer Demo/AdjustmentViewController.xib @@ -33,14 +33,15 @@ IBCocoaTouchFramework - + 274 292 - {{20, 8}, {280, 70}} + {{20, 2}, {280, 70}} + NO YES @@ -72,8 +73,9 @@ 292 - {{20, 8}, {280, 70}} + {{20, 2}, {280, 70}} + NO YES @@ -95,8 +97,9 @@ 292 - {{20, 86}, {280, 70}} + {{20, 74}, {280, 70}} + NO YES @@ -125,8 +128,9 @@ 292 - {{20, 86}, {280, 70}} + {{20, 74}, {280, 70}} + NO YES @@ -145,8 +149,9 @@ 292 - {{20, 164}, {280, 70}} + {{20, 146}, {280, 70}} + NO YES @@ -175,8 +180,9 @@ 292 - {{20, 164}, {280, 70}} + {{20, 146}, {280, 70}} + NO YES @@ -195,8 +201,9 @@ 292 - {{20, 242}, {280, 70}} + {{20, 218}, {280, 70}} + NO YES @@ -225,9 +232,10 @@ 292 - {{20, 242}, {280, 70}} + {{20, 218}, {280, 70}} - + + NO YES 7 @@ -242,12 +250,41 @@ + + + 292 + {{20, 291}, {52, 21}} + + + + _NS:328 + NO + YES + 7 + NO + IBCocoaTouchFramework + Offset + + + 1 + 10 + + 1 + 17 + + + Helvetica + 17 + 16 + + 292 - {{18, 320}, {278, 23}} + {{78, 291}, {218, 23}} - + + _NS:623 NO IBCocoaTouchFramework @@ -255,11 +292,63 @@ 0 -1 + + + 292 + {{254, 307}, {40, 21}} + + + + _NS:328 + NO + YES + 7 + NO + IBCocoaTouchFramework + 9.999 + + + 1 + 10 + 2 + + 1 + 13 + + + Helvetica + 13 + 16 + + + + + 292 + {{20, 330}, {52, 21}} + + + + _NS:328 + NO + YES + 7 + NO + IBCocoaTouchFramework + Size + + + 1 + 10 + + + 292 - {{18, 369}, {278, 23}} + {{78, 330}, {218, 23}} + + _NS:623 NO IBCocoaTouchFramework @@ -269,8 +358,32 @@ 14 60 + + + 292 + {{254, 346}, {40, 21}} + + + + _NS:328 + NO + YES + 7 + NO + IBCocoaTouchFramework + 99 + + + 1 + 10 + 2 + + + - {{0, 20}, {320, 411}} + {{0, 64}, {320, 367}} + + 3 @@ -281,6 +394,9 @@ NO + + NO + IBCocoaTouchFramework @@ -375,6 +491,22 @@ 53 + + + offsetLabel + + + + 60 + + + + pointSizeLabel + + + + 61 + settingsChanged: @@ -427,6 +559,10 @@ + + + + @@ -480,6 +616,26 @@ + + 54 + + + + + 55 + + + + + 56 + + + + + 57 + + + @@ -495,6 +651,10 @@ com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin @@ -504,7 +664,7 @@ - 53 + 61 @@ -523,54 +683,64 @@ - UILabel - UILabel - UILabel - UILabel - UILabel - UILabel - UILabel - UILabel + UILabel + UILabel + UILabel + UILabel + UILabel + UILabel + UILabel + UILabel + UILabel OBSlider + UILabel UISlider - - font1BoldItalicLabel + + font1FirstLabel + UILabel + + + font1FourthLabel UILabel - - font1BoldLabel + + font1SecondLabel UILabel - - font1ItalicLabel + + font1ThirdLabel UILabel - - font1NormalLabel + + font2FirstLabel UILabel - - font2BoldItalicLabel + + font2FourthLabel UILabel - - font2BoldLabel + + font2SecondLabel UILabel - - font2ItalicLabel + + font2ThirdLabel UILabel - - font2NormalLabel + + offsetLabel UILabel offsetSlider OBSlider + + pointSizeLabel + UILabel + pointSizeSlider UISlider diff --git a/FontReplacer Demo/AppDelegate.m b/FontReplacer Demo/AppDelegate.m index 508a7a9..8a5aa60 100644 --- a/FontReplacer Demo/AppDelegate.m +++ b/FontReplacer Demo/AppDelegate.m @@ -8,7 +8,6 @@ #import "AppDelegate.h" -#import "AdjustmentViewController.h" #import "FontsViewController.h" #import "UIFont+Replacement.h" @@ -19,14 +18,11 @@ - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions: UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UIViewController *demoViewController = [[UIViewController alloc] initWithNibName:@"DemoViewController" bundle:nil]; UIViewController *fontsViewController = [[[FontsViewController alloc] init] autorelease]; - UIViewController *adjustmentViewController = [[[AdjustmentViewController alloc] init] autorelease]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:fontsViewController]; UITabBarController *tabBarController = [[UITabBarController alloc] init]; tabBarController.delegate = self; - tabBarController.viewControllers = [NSArray arrayWithObjects:demoViewController, adjustmentViewController, navigationController, nil]; + tabBarController.viewControllers = [NSArray arrayWithObjects:demoViewController, navigationController, nil]; demoViewController.title = @"Demo"; - adjustmentViewController.title = @"Adjust"; - fontsViewController.title = @"Fonts"; if ([window respondsToSelector:@selector(setRootViewController:)]) window.rootViewController = tabBarController; else diff --git a/FontReplacer Demo/ComparatorViewController.h b/FontReplacer Demo/ComparatorViewController.h deleted file mode 100644 index 599674d..0000000 --- a/FontReplacer Demo/ComparatorViewController.h +++ /dev/null @@ -1,16 +0,0 @@ -// -// ComparatorViewController.h -// FontReplacer Demo -// -// Created by Cédric Luthi on 11.08.11. -// Copyright (c) 2011 Cédric Luthi. All rights reserved. -// - -@interface ComparatorViewController : UITableViewController -{ - NSMutableArray *results; -} - -- (id) initWithFontName:(NSString *)fontName; - -@end diff --git a/FontReplacer Demo/ComparatorViewController.m b/FontReplacer Demo/ComparatorViewController.m deleted file mode 100644 index f5ee345..0000000 --- a/FontReplacer Demo/ComparatorViewController.m +++ /dev/null @@ -1,86 +0,0 @@ -// -// ComparatorViewController.m -// FontReplacer Demo -// -// Created by Cédric Luthi on 11.08.11. -// Copyright (c) 2011 Cédric Luthi. All rights reserved. -// - -#import "ComparatorViewController.h" -#import "ComparisonResult.h" - -@implementation ComparatorViewController - -- (id) initWithFontName:(NSString *)fontName; -{ - if (!(self = [self initWithStyle:UITableViewStylePlain])) - return nil; - - self.title = fontName; - - const CGFloat fontSize = 12; - NSString *systemFamilyName = [UIFont systemFontOfSize:fontSize].familyName; - UIFont *font = [UIFont fontWithName:fontName size:fontSize]; - NSUInteger fontCount = [[UIFont fontNamesForFamilyName:font.familyName] count]; - results = [[NSMutableArray alloc] init]; - for (NSString *familyName in [[UIFont familyNames] sortedArrayUsingSelector:@selector(localizedCompare:)]) - { - NSArray *fontNames = [[UIFont fontNamesForFamilyName:familyName] sortedArrayUsingSelector:@selector(localizedCompare:)]; - if ([fontNames count] < fontCount || [familyName isEqualToString:font.familyName] || [familyName hasPrefix:systemFamilyName]) - continue; - - for (NSString *comparisonFontName in fontNames) - { - UIFont *comparisonFont = [UIFont fontWithName:comparisonFontName size:fontSize]; - // TODO: find the right formula - CGFloat score = fabsf(comparisonFont.ascender - font.ascender) + fabsf(comparisonFont.descender - font.descender) + fabsf(comparisonFont.capHeight - font.capHeight) - + fabsf(comparisonFont.xHeight - font.xHeight) + fabsf(comparisonFont.lineHeight - font.lineHeight); - ComparisonResult *result = [ComparisonResult resultWithFamilyName:familyName score:score]; - [results addObject:result]; - break; - } - } - [results sortUsingSelector:@selector(compare:)]; - - return self; -} - -- (void) dealloc -{ - [results release]; - [super dealloc]; -} - -// MARK: - Table View - -- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView -{ - return 1; -} - -- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section -{ - return [results count]; -} - -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath -{ - static NSString *CellIdentifier = @"Cell"; - - UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; - if (cell == nil) - cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; - - ComparisonResult *result = [results objectAtIndex:indexPath.row]; - cell.textLabel.text = result.familyName; - cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f", result.score]; - - return cell; -} - -- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath -{ - [self.navigationController popViewControllerAnimated:YES]; -} - -@end diff --git a/FontReplacer Demo/ComparisonResult.h b/FontReplacer Demo/ComparisonResult.h deleted file mode 100644 index a2e0f70..0000000 --- a/FontReplacer Demo/ComparisonResult.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// ComparisonResult.h -// FontReplacer Demo -// -// Created by Cédric Luthi on 13.08.11. -// Copyright (c) 2011 Cédric Luthi. All rights reserved. -// - -@interface ComparisonResult : NSObject - -+ (id) resultWithFamilyName:(NSString *)familyName score:(CGFloat)score; -- (id) initWithFamilyName:(NSString *)familyName score:(CGFloat)score; - -- (NSComparisonResult) compare:(ComparisonResult *)aResult; - -@property (nonatomic, retain) NSString *familyName; -@property (nonatomic, assign) CGFloat score; - -@end diff --git a/FontReplacer Demo/ComparisonResult.m b/FontReplacer Demo/ComparisonResult.m deleted file mode 100644 index 39e9e2f..0000000 --- a/FontReplacer Demo/ComparisonResult.m +++ /dev/null @@ -1,42 +0,0 @@ -// -// ComparisonResult.m -// FontReplacer Demo -// -// Created by Cédric Luthi on 13.08.11. -// Copyright (c) 2011 Cédric Luthi. All rights reserved. -// - -#import "ComparisonResult.h" - -@implementation ComparisonResult - -@synthesize familyName; -@synthesize score; - -+ (id) resultWithFamilyName:(NSString *)familyName score:(CGFloat)score -{ - return [[[self alloc] initWithFamilyName:familyName score:score] autorelease]; -} - -- (id) initWithFamilyName:(NSString *)aFamilyName score:(CGFloat)aScore -{ - if (!(self = [super init])) - return nil; - - self.familyName = aFamilyName; - self.score = aScore; - - return self; -} - -- (NSComparisonResult) compare:(ComparisonResult *)aResult -{ - if (aResult.score > self.score) - return NSOrderedAscending; - else if (aResult.score < self.score) - return NSOrderedDescending; - else - return NSOrderedSame; -} - -@end diff --git a/FontReplacer Demo/FontsViewController.h b/FontReplacer Demo/FontsViewController.h index 97464a8..f42c652 100644 --- a/FontReplacer Demo/FontsViewController.h +++ b/FontReplacer Demo/FontsViewController.h @@ -13,4 +13,6 @@ NSArray *familyNames; } +@property (nonatomic, retain) NSString *replacementFontName; + @end diff --git a/FontReplacer Demo/FontsViewController.m b/FontReplacer Demo/FontsViewController.m index eefd393..287f8ea 100644 --- a/FontReplacer Demo/FontsViewController.m +++ b/FontReplacer Demo/FontsViewController.m @@ -7,7 +7,14 @@ // #import "FontsViewController.h" -#import "ComparatorViewController.h" + +#import "AdjustmentViewController.h" + +@interface FontsViewController () + +- (void) reloadData; + +@end @implementation FontsViewController @@ -17,6 +24,7 @@ - (id) init return nil; familyNames = [[[UIFont familyNames] sortedArrayUsingSelector:@selector(localizedCompare:)] retain]; + self.title = @"Replacement font"; return self; } @@ -27,6 +35,36 @@ - (void) dealloc [super dealloc]; } +// MARK: - Accessors and mutators + +@synthesize replacementFontName = _replacementFontName; + +- (void) setReplacementFontName:(NSString *)replacementFontName +{ + if (_replacementFontName == replacementFontName) + return; + + [_replacementFontName release]; + _replacementFontName = [replacementFontName retain]; + + [self reloadData]; +} + +// MARK: - View lifecycle + +- (void) viewDidLoad +{ + [super viewDidLoad]; + [self reloadData]; +} + +// MARK: - Reloading the screen + +- (void) reloadData +{ + self.title = self.replacementFontName ? @"Replaced font" : @"Replacement font"; +} + // MARK: - Table View - (NSArray *) fontNamesForSection:(NSInteger)section @@ -88,8 +126,18 @@ - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath { NSString *fontName = [self fontNameAtIndexPath:indexPath]; - ComparatorViewController *comparatorViewController = [[ComparatorViewController alloc] initWithFontName:fontName]; - [self.navigationController pushViewController:comparatorViewController animated:YES]; + if (self.replacementFontName) + { + AdjustmentViewController *adjustmentViewController = [[[AdjustmentViewController alloc] initWithReplacedFontName:fontName + replacementFontName:self.replacementFontName] autorelease]; + [self.navigationController pushViewController:adjustmentViewController animated:YES]; + } + else + { + FontsViewController *fontsViewController = [[[FontsViewController alloc] init] autorelease]; + fontsViewController.replacementFontName = fontName; + [self.navigationController pushViewController:fontsViewController animated:YES]; + } } @end From a7ebd6dccc6188b1150941d98079eaa9870df68c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Mon, 16 Jan 2012 07:22:11 +0100 Subject: [PATCH 10/13] Loading from and saving to a plist --- FontReplacer Demo/AdjustmentViewController.h | 1 + FontReplacer Demo/AdjustmentViewController.m | 74 +++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/FontReplacer Demo/AdjustmentViewController.h b/FontReplacer Demo/AdjustmentViewController.h index 20cdb8b..5beab36 100644 --- a/FontReplacer Demo/AdjustmentViewController.h +++ b/FontReplacer Demo/AdjustmentViewController.h @@ -30,5 +30,6 @@ @property (nonatomic, retain) IBOutlet UILabel *pointSizeLabel; - (IBAction) settingsChanged:(id)sender; +- (IBAction) save:(id)sender; @end diff --git a/FontReplacer Demo/AdjustmentViewController.m b/FontReplacer Demo/AdjustmentViewController.m index 08ebbd1..71901cf 100644 --- a/FontReplacer Demo/AdjustmentViewController.m +++ b/FontReplacer Demo/AdjustmentViewController.m @@ -21,6 +21,10 @@ - (void) reloadData; - (void) reloadReplacedFontLabels; - (void) reloadReplacementFontLabels; +- (void) loadSettings; +- (void) saveSettings; +- (NSString *)settingsFilePath; + @end @implementation AdjustmentViewController @@ -87,7 +91,10 @@ - (void) viewDidLoad [super viewDidLoad]; self.pointSizeSlider.value = self.font1FirstLabel.font.pointSize; - + self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave + target:self + action:@selector(save:)] autorelease]; + [self loadSettings]; [self reloadData]; } @@ -148,6 +155,65 @@ - (void) reloadReplacementFontLabels [self.font2FourthLabel setNeedsDisplay]; } +// MARK: - Loading from and saving to a plist + +- (void) loadSettings +{ + NSString *settingsFilePath = [self settingsFilePath]; + NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:settingsFilePath]; + if (! settings) { + self.offsetSlider.value = 0.f; + return; + } + + NSDictionary *replacementDictionary = [settings objectForKey:@"ReplacementFonts"]; + if (! replacementDictionary) { + self.offsetSlider.value = 0.f; + return; + } + + NSDictionary *replacementInfoDictionary = [replacementDictionary objectForKey:self.replacedFontName]; + if (! replacementInfoDictionary) { + self.offsetSlider.value = 0.f; + return; + } + + self.offsetSlider.value = [[replacementInfoDictionary objectForKey:@"Offset"] floatValue]; +} + +- (void)saveSettings +{ + NSString *settingsFilePath = [self settingsFilePath]; + NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithContentsOfFile:settingsFilePath] ?: [NSMutableDictionary dictionary]; + + // If the replacement font was already used, remove the corresponding entry + NSMutableDictionary *replacementDictionary = [NSMutableDictionary dictionaryWithDictionary:[settings objectForKey:@"ReplacementFonts"]]; + for (NSString *replacedFontName in [replacementDictionary allKeys]) { + NSDictionary *replacementInfoDictionary = [replacementDictionary objectForKey:replacedFontName]; + if ([self.replacementFontName isEqualToString:[replacementInfoDictionary objectForKey:@"Name"]]) { + [replacementDictionary removeObjectForKey:replacedFontName]; + } + } + + NSDictionary *replacementInfoDictionary = nil; + if (self.offsetSlider.value != 0) { + replacementInfoDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.replacementFontName, @"Name", + [NSNumber numberWithFloat:self.offsetSlider.value], @"Offset", nil]; + } + else { + replacementInfoDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.replacementFontName, @"Name", nil]; + } + [replacementDictionary setObject:replacementInfoDictionary forKey:self.replacedFontName]; + [settings setObject:replacementDictionary forKey:@"ReplacementFonts"]; + [settings writeToFile:settingsFilePath atomically:NO]; +} + +- (NSString *)settingsFilePath +{ + NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; + return [documentPath stringByAppendingPathComponent:@"Settings.plist"]; +} + // MARK: - Event callbacks - (IBAction) settingsChanged:(id)sender @@ -155,4 +221,10 @@ - (IBAction) settingsChanged:(id)sender [self reloadData]; } +- (IBAction) save:(id)sender +{ + [self saveSettings]; + [self.navigationController popToRootViewControllerAnimated:YES]; +} + @end From a8f9cb8fc862b73e98c4474c4c180e071dfa11fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Mon, 16 Jan 2012 09:00:45 +0100 Subject: [PATCH 11/13] Ensuring that fonts are replaced on selected screens only --- FontReplacer Demo/AdjustmentViewController.m | 5 +++++ FontReplacer Demo/AppDelegate.h | 2 +- FontReplacer Demo/AppDelegate.m | 17 ----------------- FontReplacer Demo/FontsViewController.m | 14 ++++++++++++-- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/FontReplacer Demo/AdjustmentViewController.m b/FontReplacer Demo/AdjustmentViewController.m index 71901cf..587d485 100644 --- a/FontReplacer Demo/AdjustmentViewController.m +++ b/FontReplacer Demo/AdjustmentViewController.m @@ -95,6 +95,11 @@ - (void) viewDidLoad target:self action:@selector(save:)] autorelease]; [self loadSettings]; +} + +- (void) viewWillAppear:(BOOL)animated +{ + [super viewWillAppear:animated]; [self reloadData]; } diff --git a/FontReplacer Demo/AppDelegate.h b/FontReplacer Demo/AppDelegate.h index d378a25..ac05cd4 100644 --- a/FontReplacer Demo/AppDelegate.h +++ b/FontReplacer Demo/AppDelegate.h @@ -6,6 +6,6 @@ // Copyright (c) 2011 Cédric Luthi. All rights reserved. // -@interface AppDelegate : UIResponder +@interface AppDelegate : UIResponder @end diff --git a/FontReplacer Demo/AppDelegate.m b/FontReplacer Demo/AppDelegate.m index 8a5aa60..60c622d 100644 --- a/FontReplacer Demo/AppDelegate.m +++ b/FontReplacer Demo/AppDelegate.m @@ -9,7 +9,6 @@ #import "AppDelegate.h" #import "FontsViewController.h" -#import "UIFont+Replacement.h" @implementation AppDelegate @@ -20,7 +19,6 @@ - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions: UIViewController *fontsViewController = [[[FontsViewController alloc] init] autorelease]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:fontsViewController]; UITabBarController *tabBarController = [[UITabBarController alloc] init]; - tabBarController.delegate = self; tabBarController.viewControllers = [NSArray arrayWithObjects:demoViewController, navigationController, nil]; demoViewController.title = @"Demo"; if ([window respondsToSelector:@selector(setRootViewController:)]) @@ -31,19 +29,4 @@ - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions: return YES; } -- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController -{ - if ([viewController isKindOfClass:[UINavigationController class]]) - { - [UIFont setReplacementDictionary:nil]; - } - else - { - NSDictionary *plistDictionary = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"ReplacementFonts"]; - [UIFont setReplacementDictionary:plistDictionary]; - } - - return YES; -} - @end diff --git a/FontReplacer Demo/FontsViewController.m b/FontReplacer Demo/FontsViewController.m index 287f8ea..e7e0ed8 100644 --- a/FontReplacer Demo/FontsViewController.m +++ b/FontReplacer Demo/FontsViewController.m @@ -9,6 +9,7 @@ #import "FontsViewController.h" #import "AdjustmentViewController.h" +#import "UIFont+Replacement.h" @interface FontsViewController () @@ -52,17 +53,26 @@ - (void) setReplacementFontName:(NSString *)replacementFontName // MARK: - View lifecycle -- (void) viewDidLoad +- (void) viewWillAppear:(BOOL)animated { - [super viewDidLoad]; + [super viewWillAppear:animated]; + [UIFont setReplacementDictionary:nil]; [self reloadData]; } +- (void) viewWillDisappear:(BOOL)animated +{ + [super viewWillDisappear:animated]; + NSDictionary *plistDictionary = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"ReplacementFonts"]; + [UIFont setReplacementDictionary:plistDictionary]; +} + // MARK: - Reloading the screen - (void) reloadData { self.title = self.replacementFontName ? @"Replaced font" : @"Replacement font"; + [self.tableView reloadData]; } // MARK: - Table View From 9f92bbe74f0027ea0ae793deb0344943ffbe314d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Mon, 16 Jan 2012 09:08:56 +0100 Subject: [PATCH 12/13] Simulator: Saving setting plist to the desktop --- FontReplacer Demo/AdjustmentViewController.m | 22 ++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/FontReplacer Demo/AdjustmentViewController.m b/FontReplacer Demo/AdjustmentViewController.m index 587d485..1aa4b14 100644 --- a/FontReplacer Demo/AdjustmentViewController.m +++ b/FontReplacer Demo/AdjustmentViewController.m @@ -9,6 +9,7 @@ #import "AdjustmentViewController.h" #import "UIFont+Replacement.h" +#import @interface AdjustmentViewController () @@ -23,7 +24,9 @@ - (void) reloadReplacementFontLabels; - (void) loadSettings; - (void) saveSettings; -- (NSString *)settingsFilePath; + +- (NSString *) homeDirectory; +- (NSString *) settingsFilePath; @end @@ -186,7 +189,7 @@ - (void) loadSettings self.offsetSlider.value = [[replacementInfoDictionary objectForKey:@"Offset"] floatValue]; } -- (void)saveSettings +- (void) saveSettings { NSString *settingsFilePath = [self settingsFilePath]; NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithContentsOfFile:settingsFilePath] ?: [NSMutableDictionary dictionary]; @@ -213,10 +216,21 @@ - (void)saveSettings [settings writeToFile:settingsFilePath atomically:NO]; } +- (NSString *) homeDirectory +{ + NSString *logname = [NSString stringWithCString:getenv("LOGNAME") encoding:NSUTF8StringEncoding]; + struct passwd *pw = getpwnam([logname UTF8String]); + return pw ? [NSString stringWithCString:pw->pw_dir encoding:NSUTF8StringEncoding] : [@"/Users" stringByAppendingPathComponent:logname]; +} + - (NSString *)settingsFilePath { - NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; - return [documentPath stringByAppendingPathComponent:@"Settings.plist"]; +#if TARGET_IPHONE_SIMULATOR + NSString *saveDirectoryPath = [NSString stringWithFormat:@"%@/Desktop/", [self homeDirectory]]; +#else + NSString *saveDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; +#endif + return [saveDirectoryPath stringByAppendingPathComponent:@"FontReplacerSettings.plist"]; } // MARK: - Event callbacks From 678c96d6c039a6a72023d400598cd2699dbcec39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20De=CC=81fago?= Date: Mon, 16 Jan 2012 10:09:01 +0100 Subject: [PATCH 13/13] Just some polishing (coding style, naming, comments) --- FontReplacer Demo/AdjustmentViewController.h | 16 +- FontReplacer Demo/AdjustmentViewController.m | 82 +++++----- .../AdjustmentViewController.xib | 153 +++++++++--------- FontReplacer Demo/FontsViewController.m | 5 +- UIFont+Replacement/UIFont+Replacement.m | 12 +- 5 files changed, 140 insertions(+), 128 deletions(-) diff --git a/FontReplacer Demo/AdjustmentViewController.h b/FontReplacer Demo/AdjustmentViewController.h index 5beab36..a499f98 100644 --- a/FontReplacer Demo/AdjustmentViewController.h +++ b/FontReplacer Demo/AdjustmentViewController.h @@ -14,14 +14,14 @@ - (id) initWithReplacedFontName:(NSString *)replacedFontName replacementFontName:(NSString *)replacementFontName; -@property (nonatomic, retain) IBOutlet UILabel *font1FirstLabel; -@property (nonatomic, retain) IBOutlet UILabel *font2FirstLabel; -@property (nonatomic, retain) IBOutlet UILabel *font1SecondLabel; -@property (nonatomic, retain) IBOutlet UILabel *font2SecondLabel; -@property (nonatomic, retain) IBOutlet UILabel *font1ThirdLabel; -@property (nonatomic, retain) IBOutlet UILabel *font2ThirdLabel; -@property (nonatomic, retain) IBOutlet UILabel *font1FourthLabel; -@property (nonatomic, retain) IBOutlet UILabel *font2FourthLabel; +@property (nonatomic, retain) IBOutlet UILabel *replacementFontFirstLabel; +@property (nonatomic, retain) IBOutlet UILabel *replacedFontFirstLabel; +@property (nonatomic, retain) IBOutlet UILabel *replacementFontSecondLabel; +@property (nonatomic, retain) IBOutlet UILabel *replacedFontSecondLabel; +@property (nonatomic, retain) IBOutlet UILabel *replacementFontThirdLabel; +@property (nonatomic, retain) IBOutlet UILabel *replacedFontThirdLabel; +@property (nonatomic, retain) IBOutlet UILabel *replacementFontFourthLabel; +@property (nonatomic, retain) IBOutlet UILabel *replacedFontFourthLabel; @property (nonatomic, retain) IBOutlet OBSlider *offsetSlider; @property (nonatomic, retain) IBOutlet UILabel *offsetLabel; diff --git a/FontReplacer Demo/AdjustmentViewController.m b/FontReplacer Demo/AdjustmentViewController.m index 1aa4b14..6c63e96 100644 --- a/FontReplacer Demo/AdjustmentViewController.m +++ b/FontReplacer Demo/AdjustmentViewController.m @@ -35,14 +35,14 @@ @implementation AdjustmentViewController @synthesize replacedFontName = _replacedFontName; @synthesize replacementFontName = _replacementFontName; -@synthesize font1FirstLabel = _font1FirstLabel; -@synthesize font2FirstLabel = _font2FirstLabel; -@synthesize font1SecondLabel = _font1SecondLabel; -@synthesize font2SecondLabel = _font2SecondLabel; -@synthesize font1ThirdLabel = _font1ThirdLabel; -@synthesize font2ThirdLabel = _font2ThirdLabel; -@synthesize font1FourthLabel = _font1FourthLabel; -@synthesize font2FourthLabel = _font2FourthLabel; +@synthesize replacementFontFirstLabel = _font1FirstLabel; +@synthesize replacedFontFirstLabel = _font2FirstLabel; +@synthesize replacementFontSecondLabel = _font1SecondLabel; +@synthesize replacedFontSecondLabel = _font2SecondLabel; +@synthesize replacementFontThirdLabel = _font1ThirdLabel; +@synthesize replacedFontThirdLabel = _font2ThirdLabel; +@synthesize replacementFontFourthLabel = _font1FourthLabel; +@synthesize replacedFontFourthLabel = _font2FourthLabel; @synthesize offsetSlider = _factorSlider; @synthesize offsetLabel = _offsetLabel; @@ -73,14 +73,14 @@ - (void) dealloc - (void) releaseViews { - self.font1FirstLabel = nil; - self.font2FirstLabel = nil; - self.font1SecondLabel = nil; - self.font2SecondLabel = nil; - self.font1ThirdLabel = nil; - self.font2ThirdLabel = nil; - self.font1FourthLabel = nil; - self.font2FourthLabel = nil; + self.replacementFontFirstLabel = nil; + self.replacedFontFirstLabel = nil; + self.replacementFontSecondLabel = nil; + self.replacedFontSecondLabel = nil; + self.replacementFontThirdLabel = nil; + self.replacedFontThirdLabel = nil; + self.replacementFontFourthLabel = nil; + self.replacedFontFourthLabel = nil; self.offsetSlider = nil; self.offsetLabel = nil; self.pointSizeSlider = nil; @@ -93,7 +93,7 @@ - (void) viewDidLoad { [super viewDidLoad]; - self.pointSizeSlider.value = self.font1FirstLabel.font.pointSize; + self.pointSizeSlider.value = self.replacementFontFirstLabel.font.pointSize; self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save:)] autorelease]; @@ -137,10 +137,10 @@ - (void) reloadReplacedFontLabels { [UIFont setReplacementDictionary:nil]; - self.font1FirstLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; - self.font1SecondLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; - self.font1ThirdLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; - self.font1FourthLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.replacedFontFirstLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.replacedFontSecondLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.replacedFontThirdLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.replacedFontFourthLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; } - (void) reloadReplacementFontLabels @@ -151,16 +151,16 @@ - (void) reloadReplacementFontLabels forKey:self.replacedFontName]; [UIFont setReplacementDictionary:replacementDictionary]; - self.font2FirstLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; - self.font2SecondLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; - self.font2ThirdLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; - self.font2FourthLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.replacementFontFirstLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.replacementFontSecondLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.replacementFontThirdLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; + self.replacementFontFourthLabel.font = [UIFont fontWithName:self.replacedFontName size:floorf(self.pointSizeSlider.value)]; // Force a refresh - [self.font2FirstLabel setNeedsDisplay]; - [self.font2SecondLabel setNeedsDisplay]; - [self.font2ThirdLabel setNeedsDisplay]; - [self.font2FourthLabel setNeedsDisplay]; + [self.replacementFontFirstLabel setNeedsDisplay]; + [self.replacementFontSecondLabel setNeedsDisplay]; + [self.replacementFontThirdLabel setNeedsDisplay]; + [self.replacementFontFourthLabel setNeedsDisplay]; } // MARK: - Loading from and saving to a plist @@ -169,19 +169,22 @@ - (void) loadSettings { NSString *settingsFilePath = [self settingsFilePath]; NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:settingsFilePath]; - if (! settings) { + if (!settings) + { self.offsetSlider.value = 0.f; return; } NSDictionary *replacementDictionary = [settings objectForKey:@"ReplacementFonts"]; - if (! replacementDictionary) { + if (!replacementDictionary) + { self.offsetSlider.value = 0.f; return; } NSDictionary *replacementInfoDictionary = [replacementDictionary objectForKey:self.replacedFontName]; - if (! replacementInfoDictionary) { + if (!replacementInfoDictionary) + { self.offsetSlider.value = 0.f; return; } @@ -194,21 +197,26 @@ - (void) saveSettings NSString *settingsFilePath = [self settingsFilePath]; NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithContentsOfFile:settingsFilePath] ?: [NSMutableDictionary dictionary]; - // If the replacement font was already used, remove the corresponding entry + // If the replacement font was already used, remove the existing entry NSMutableDictionary *replacementDictionary = [NSMutableDictionary dictionaryWithDictionary:[settings objectForKey:@"ReplacementFonts"]]; - for (NSString *replacedFontName in [replacementDictionary allKeys]) { + for (NSString *replacedFontName in [replacementDictionary allKeys]) + { NSDictionary *replacementInfoDictionary = [replacementDictionary objectForKey:replacedFontName]; - if ([self.replacementFontName isEqualToString:[replacementInfoDictionary objectForKey:@"Name"]]) { + if ([self.replacementFontName isEqualToString:[replacementInfoDictionary objectForKey:@"Name"]]) + { [replacementDictionary removeObjectForKey:replacedFontName]; } } + // Save settings NSDictionary *replacementInfoDictionary = nil; - if (self.offsetSlider.value != 0) { + if (self.offsetSlider.value != 0) + { replacementInfoDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.replacementFontName, @"Name", [NSNumber numberWithFloat:self.offsetSlider.value], @"Offset", nil]; } - else { + else + { replacementInfoDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.replacementFontName, @"Name", nil]; } [replacementDictionary setObject:replacementInfoDictionary forKey:self.replacedFontName]; diff --git a/FontReplacer Demo/AdjustmentViewController.xib b/FontReplacer Demo/AdjustmentViewController.xib index 93e09e7..0b8ffba 100644 --- a/FontReplacer Demo/AdjustmentViewController.xib +++ b/FontReplacer Demo/AdjustmentViewController.xib @@ -429,83 +429,83 @@ - font1FirstLabel + offsetLabel - + - 46 + 60 - font2FirstLabel + pointSizeLabel - + - 47 + 61 - font1SecondLabel + replacedFontFirstLabel - + - 48 + 70 - font2SecondLabel + replacementFontFirstLabel - + - 49 + 71 - font1ThirdLabel + replacedFontSecondLabel - + - 50 + 72 - font2ThirdLabel + replacementFontSecondLabel - + - 51 + 73 - font1FourthLabel + replacedFontThirdLabel - + - 52 + 74 - font2FourthLabel + replacementFontThirdLabel - + - 53 + 75 - offsetLabel + replacedFontFourthLabel - + - 60 + 76 - pointSizeLabel + replacementFontFourthLabel - + - 61 + 77 @@ -664,86 +664,89 @@ - 61 + 77 AdjustmentViewController UIViewController - - settingsChanged: - id - - - settingsChanged: - + + id + id + + + + save: + id + + settingsChanged: id - + - UILabel - UILabel - UILabel - UILabel - UILabel - UILabel - UILabel - UILabel UILabel OBSlider UILabel UISlider + UILabel + UILabel + UILabel + UILabel + UILabel + UILabel + UILabel + UILabel - - font1FirstLabel + + offsetLabel UILabel - - font1FourthLabel - UILabel + + offsetSlider + OBSlider - - font1SecondLabel + + pointSizeLabel UILabel - - font1ThirdLabel - UILabel + + pointSizeSlider + UISlider - - font2FirstLabel + + replacedFontFirstLabel UILabel - - font2FourthLabel + + replacedFontFourthLabel UILabel - - font2SecondLabel + + replacedFontSecondLabel UILabel - - font2ThirdLabel + + replacedFontThirdLabel UILabel - - offsetLabel + + replacementFontFirstLabel UILabel - - offsetSlider - OBSlider + + replacementFontFourthLabel + UILabel - - pointSizeLabel + + replacementFontSecondLabel UILabel - - pointSizeSlider - UISlider + + replacementFontThirdLabel + UILabel diff --git a/FontReplacer Demo/FontsViewController.m b/FontReplacer Demo/FontsViewController.m index e7e0ed8..8b9c2bb 100644 --- a/FontReplacer Demo/FontsViewController.m +++ b/FontReplacer Demo/FontsViewController.m @@ -57,7 +57,10 @@ - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [UIFont setReplacementDictionary:nil]; - [self reloadData]; + + // Fixes a strange issue: If the table view is reloaded directly, fonts are not updated correctly. Doing it + // in the next run loop iteration fixes this issue + [self performSelector:@selector(reloadData) withObject:nil afterDelay:0.]; } - (void) viewWillDisappear:(BOOL)animated diff --git a/UIFont+Replacement/UIFont+Replacement.m b/UIFont+Replacement/UIFont+Replacement.m index 59c7e62..df96dd4 100644 --- a/UIFont+Replacement/UIFont+Replacement.m +++ b/UIFont+Replacement/UIFont+Replacement.m @@ -78,23 +78,21 @@ - (CGFloat) replacement_ascender NSString *fontName = [self fontName]; CGFloat ascender = [self replacement_ascender]; - // The receiver is not replacing any font. Return original ascender value + // The receiver is not replacing any font. Return the original ascender value NSString *replacedFontName = [inverseReplacementDictionary objectForKey:fontName]; if (!replacedFontName) - { return ascender; - } - // The receiver is replacing another font: To access the replaced font, we have to remove the replacement dictionary + // The receiver is replacing another font: To access the replaced UIFont object, we have to remove the replacement dictionary // temporarily NSDictionary *originalReplacementDictionary = [[[UIFont replacementDictionary] retain] autorelease]; [UIFont setReplacementDictionary:nil]; UIFont *replacedFont = [UIFont fontWithName:replacedFontName size:self.pointSize]; [UIFont setReplacementDictionary:originalReplacementDictionary]; - // Adjust the receiver ascender value. A good default behavior is to match the ascender value of the replacing - // font to match the one of the replaced font. If this default behavior is not convincing enough, an offset - // can be provided + // Adjust the receiver ascender value. A good default behavior is to have the ascender value of the replacing + // font match the one of the replaced font. If this default behavior is not convincing enough, an offset + // can be optionally provided in the plist settings return [replacedFont replacement_ascender] + self.pointSize * [UIFont offsetForFontWithName:replacedFontName]; }