Cleans up code that generates HTML labels.

Adds Label for speed.
Makes the Monster Card refresh the monster from CoreData when the view is shown.
This commit is contained in:
2020-09-26 15:09:46 -07:00
parent d041105e1e
commit caa1be50cf
5 changed files with 77 additions and 18 deletions

View File

@@ -193,7 +193,7 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="pZa-ia-7UT">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="pZa-ia-7UT">
<rect key="frame" x="8" y="96" width="398" height="14.5"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<color key="textColor" systemColor="secondaryLabelColor"/>
@@ -205,14 +205,20 @@
<constraint firstAttribute="height" constant="10" id="1yt-3D-aZx"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="p05-uG-AlV">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ae7-YW-3xk">
<rect key="frame" x="8" y="161.5" width="398" height="17"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="p05-uG-AlV">
<rect key="frame" x="8" y="136.5" width="398" height="17"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ae7-YW-3xk">
<rect key="frame" x="8" y="161.5" width="398" height="17"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0HK-T4-KQW" userLabel="Monster Speed">
<rect key="frame" x="8" y="186.5" width="398" height="17"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
@@ -221,17 +227,20 @@
<viewLayoutGuide key="safeArea" id="WIX-Yu-LXJ"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="WIX-Yu-LXJ" firstAttribute="trailing" secondItem="0HK-T4-KQW" secondAttribute="trailing" constant="8" id="08t-ag-Qe6"/>
<constraint firstItem="WIX-Yu-LXJ" firstAttribute="trailing" secondItem="pZa-ia-7UT" secondAttribute="trailing" constant="8" id="8F1-nO-xrO"/>
<constraint firstItem="pZa-ia-7UT" firstAttribute="leading" secondItem="WIX-Yu-LXJ" secondAttribute="leading" constant="8" id="9Yo-N3-VeP"/>
<constraint firstItem="Ae7-YW-3xk" firstAttribute="top" secondItem="p05-uG-AlV" secondAttribute="bottom" constant="8" id="Dyg-fV-XyM"/>
<constraint firstItem="pZa-ia-7UT" firstAttribute="top" secondItem="WIX-Yu-LXJ" secondAttribute="top" constant="8" id="GZ7-bL-EbS"/>
<constraint firstItem="WIX-Yu-LXJ" firstAttribute="trailing" secondItem="p05-uG-AlV" secondAttribute="trailing" constant="8" id="JQZ-8U-oYy"/>
<constraint firstItem="0HK-T4-KQW" firstAttribute="leading" secondItem="WIX-Yu-LXJ" secondAttribute="leading" constant="8" id="NhK-Ck-rZi"/>
<constraint firstItem="Ae7-YW-3xk" firstAttribute="leading" secondItem="WIX-Yu-LXJ" secondAttribute="leading" constant="8" id="S9u-eI-fvZ"/>
<constraint firstItem="p05-uG-AlV" firstAttribute="leading" secondItem="WIX-Yu-LXJ" secondAttribute="leading" constant="8" id="Xz5-d5-Czj"/>
<constraint firstItem="WIX-Yu-LXJ" firstAttribute="trailing" secondItem="WYM-ya-Yje" secondAttribute="trailing" constant="8" id="aMh-HD-dBC"/>
<constraint firstItem="WIX-Yu-LXJ" firstAttribute="trailing" secondItem="Ae7-YW-3xk" secondAttribute="trailing" constant="8" id="fhK-dn-EfH"/>
<constraint firstItem="p05-uG-AlV" firstAttribute="top" secondItem="WYM-ya-Yje" secondAttribute="bottom" constant="8" id="k9C-fg-kcY"/>
<constraint firstItem="WYM-ya-Yje" firstAttribute="top" secondItem="pZa-ia-7UT" secondAttribute="bottom" constant="8" id="mO1-0h-hFW"/>
<constraint firstItem="0HK-T4-KQW" firstAttribute="top" secondItem="Ae7-YW-3xk" secondAttribute="bottom" constant="8" id="t7c-uP-x4v"/>
<constraint firstItem="WYM-ya-Yje" firstAttribute="leading" secondItem="WIX-Yu-LXJ" secondAttribute="leading" constant="8" id="wB4-zH-APV"/>
</constraints>
</view>
@@ -246,6 +255,7 @@
<outlet property="monsterArmorClass" destination="p05-uG-AlV" id="ISg-8R-AnX"/>
<outlet property="monsterHitPoints" destination="Ae7-YW-3xk" id="3og-CQ-jGe"/>
<outlet property="monsterMeta" destination="pZa-ia-7UT" id="QEV-cs-IEk"/>
<outlet property="monsterSpeed" destination="0HK-T4-KQW" id="V8T-ZK-2aB"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="lvO-c7-FKV" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>

View File

@@ -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

View File

@@ -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 {

View File

@@ -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;

View File

@@ -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:@"<span style=\"font-family: %@; font-size: %@; color: %@;\">%@</span>", 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:@"<span style=\"font-family: helvetica; font-size: 12pt; color: #9B2818;\"><b>Armor Class</b> %@</span>", armorClassDescription]];
self.monsterArmorClass.attributedText = [HTMLHelper attributedStringFromHTML:makeHTMLFragmentString(@"<b>Armor Class</b> %@</span>", armorClassDescription)];
}
}
if (self.monsterHitPoints != nil) {
@@ -57,7 +76,15 @@
if (hitPointsDescription == nil) {
self.monsterHitPoints.text = @"";
} else {
self.monsterHitPoints.attributedText = [HTMLHelper attributedStringFromHTML:[NSString stringWithFormat:@"<span style=\"font-family: helvetica; font-size: 12pt; color: #9B2818;\"><b>Hit Points</b> %@</span>", hitPointsDescription]];
self.monsterHitPoints.attributedText = [HTMLHelper attributedStringFromHTML:makeHTMLFragmentString(@"<b>Hit Points</b> %@", hitPointsDescription)];
}
}
if (self.monsterSpeed != nil) {
NSString *speedDescription = self.monster.speedDescription;
if (speedDescription == nil) {
self.monsterSpeed.text = @"";
} else {
self.monsterSpeed.attributedText = [HTMLHelper attributedStringFromHTML:makeHTMLFragmentString(@"<b>Speed</b> %@", speedDescription)];
}
}
}