diff --git a/MonsterCards/Base.lproj/Main.storyboard b/MonsterCards/Base.lproj/Main.storyboard index 6d306a4..063036d 100644 --- a/MonsterCards/Base.lproj/Main.storyboard +++ b/MonsterCards/Base.lproj/Main.storyboard @@ -193,7 +193,7 @@ - + @@ -205,14 +205,20 @@ - + + + + + + + - - + + @@ -221,17 +227,20 @@ + + + @@ -246,6 +255,7 @@ + diff --git a/MonsterCards/Helpers/HTMLHelper.m b/MonsterCards/Helpers/HTMLHelper.m index 9755d3c..1f7c208 100644 --- a/MonsterCards/Helpers/HTMLHelper.m +++ b/MonsterCards/Helpers/HTMLHelper.m @@ -12,11 +12,13 @@ @implementation HTMLHelper + (NSAttributedString*)attributedStringFromHTML:(NSString *)htmlString { - - return [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES] - options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, - NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} - documentAttributes:nil error:nil]; + NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; + NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, + NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}; + return [[NSAttributedString alloc] initWithData:data + options:options + documentAttributes:nil + error:nil]; } @end diff --git a/MonsterCards/Models/Monster.m b/MonsterCards/Models/Monster.m index d74245f..9b77cd8 100644 --- a/MonsterCards/Models/Monster.m +++ b/MonsterCards/Models/Monster.m @@ -278,9 +278,28 @@ NSString* const kMonsterSizeGargantuan = @"gargantuan"; } } -//getSpeedText -(NSString*)speedDescription { - @throw [[NSException alloc] initWithName:@"unimplemented" reason:@"Method not implemented." userInfo:nil]; + if (self.hasCustomSpeed) { + return self.customSpeed; + } else { + NSMutableArray* parts = [[NSMutableArray alloc] init]; + if (self.baseSpeed > 0) { + [parts addObject:[NSString stringWithFormat:@"%d ft.", self.baseSpeed]]; + } + if (self.burrowSpeed > 0) { + [parts addObject:[NSString stringWithFormat:@"burrow %d ft.", self.burrowSpeed]]; + } + if (self.climbSpeed > 0) { + [parts addObject:[NSString stringWithFormat:@"climb %d ft.", self.climbSpeed]]; + } + if (self.flySpeed > 0) { + [parts addObject:[NSString stringWithFormat:@"fly %d ft.%@", self.flySpeed, self.canHover ? @" (hover)" : @""]]; + } + if (self.swimSpeed > 0) { + [parts addObject:[NSString stringWithFormat:@"swim %d ft.", self.swimSpeed]]; + } + return [parts componentsJoinedByString:@" "]; + } } -(NSString*)strengthDescription { diff --git a/MonsterCards/Views/MonsterViewController.h b/MonsterCards/Views/MonsterViewController.h index c04af53..c04657d 100644 --- a/MonsterCards/Views/MonsterViewController.h +++ b/MonsterCards/Views/MonsterViewController.h @@ -17,6 +17,7 @@ NS_ASSUME_NONNULL_BEGIN @property (weak, nonatomic) IBOutlet UILabel *monsterMeta; @property (weak, nonatomic) IBOutlet UILabel *monsterArmorClass; @property (weak, nonatomic) IBOutlet UILabel *monsterHitPoints; +@property (weak, nonatomic) IBOutlet UILabel *monsterSpeed; @property Monster* monster; diff --git a/MonsterCards/Views/MonsterViewController.m b/MonsterCards/Views/MonsterViewController.m index e5e1583..f9a5ebc 100644 --- a/MonsterCards/Views/MonsterViewController.m +++ b/MonsterCards/Views/MonsterViewController.m @@ -9,24 +9,43 @@ #import "MonsterViewController.h" #import "EditMonsterViewController.h" #import "HTMLHelper.h" +#import "AppDelegate.h" @interface MonsterViewController () @end -@implementation MonsterViewController +NSString *const defaultFontFamily = @"helvetica"; +NSString *const defaultFontSize = @"12pt"; +NSString *const defaultTextColor = @"#9B2818"; + +NSString* makeHTMLFragmentString(NSString* format, ...) { + va_list args; + va_start(args, format); + NSString *childString = [[NSString alloc] initWithFormat:format arguments:args]; + va_end(args); + + NSString *formattedString = [NSString stringWithFormat:@"%@", defaultFontFamily, defaultFontSize, defaultTextColor, childString]; + return formattedString; +} + +@implementation MonsterViewController { + NSManagedObjectContext *_context; +} - (void)viewDidLoad { [super viewDidLoad]; + AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate; + _context = appDelegate.persistentContainer.viewContext; +} + +- (void)viewWillAppear:(BOOL)animated { + [_context refreshObject:self.monster mergeChanges:NO]; if (self.monsterName != nil) { self.monsterName.text = self.monster.name; } else if (self.navigationItem != nil) { self.navigationItem.title = self.monster.name; } -} - -- (void)viewWillAppear:(BOOL)animated { - // TODO: get the latest version of this monster from CoreData if (self.monsterName != nil) { self.monsterName.text = self.monster.name; } else if (self.navigationItem != nil) { @@ -49,7 +68,7 @@ if (armorClassDescription == nil) { self.monsterArmorClass.text = @""; } else { - self.monsterArmorClass.attributedText = [HTMLHelper attributedStringFromHTML:[NSString stringWithFormat:@"Armor Class %@", armorClassDescription]]; + self.monsterArmorClass.attributedText = [HTMLHelper attributedStringFromHTML:makeHTMLFragmentString(@"Armor Class %@", armorClassDescription)]; } } if (self.monsterHitPoints != nil) { @@ -57,7 +76,15 @@ if (hitPointsDescription == nil) { self.monsterHitPoints.text = @""; } else { - self.monsterHitPoints.attributedText = [HTMLHelper attributedStringFromHTML:[NSString stringWithFormat:@"Hit Points %@", hitPointsDescription]]; + self.monsterHitPoints.attributedText = [HTMLHelper attributedStringFromHTML:makeHTMLFragmentString(@"Hit Points %@", hitPointsDescription)]; + } + } + if (self.monsterSpeed != nil) { + NSString *speedDescription = self.monster.speedDescription; + if (speedDescription == nil) { + self.monsterSpeed.text = @""; + } else { + self.monsterSpeed.attributedText = [HTMLHelper attributedStringFromHTML:makeHTMLFragmentString(@"Speed %@", speedDescription)]; } } }