Skip to content

Commit

Permalink
Fixed support for using system font (using system as font name or o…
Browse files Browse the repository at this point in the history
…mitting font name entirely), and added support for using bold and italic system fonts (`systemBold` and `systemItalic`).
  • Loading branch information
tolo committed Feb 22, 2019
1 parent f33639a commit 1b6b18a
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#Changes

##Version 1.5.5

### Fixes/changes
* Fixed support for using system font (using `system` as font name or omitting font name entirely), and added support for using bold and italic system fonts (`systemBold` and `systemItalic`).


##Version 1.5.3

Expand Down
12 changes: 12 additions & 0 deletions InterfaCSS Tests/ISSStyleSheetParserTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,18 @@ - (void) testFontPropertyValues {

value = [[self getPropertyValuesWithNames:@[@"font"] fromStyleClass:@"font7"] firstObject];
XCTAssertEqualObjects(value, [UIFont fontWithName:@"Times New Roman" size:5], @"Unexpected font value");

value = [[self getPropertyValuesWithNames:@[@"font"] fromStyleClass:@"font8"] firstObject];
XCTAssertEqualObjects(value, [UIFont systemFontOfSize:42], @"Unexpected font value");

value = [[self getPropertyValuesWithNames:@[@"font"] fromStyleClass:@"font9"] firstObject];
XCTAssertEqualObjects(value, [UIFont systemFontOfSize:42], @"Unexpected font value");

value = [[self getPropertyValuesWithNames:@[@"font"] fromStyleClass:@"font10"] firstObject];
XCTAssertEqualObjects(value, [UIFont italicSystemFontOfSize:42], @"Unexpected font value");

value = [[self getPropertyValuesWithNames:@[@"font"] fromStyleClass:@"font11"] firstObject];
XCTAssertEqualObjects(value, [UIFont boldSystemFontOfSize:42], @"Unexpected font value");
}

- (void) testImagePropertyValue {
Expand Down
13 changes: 12 additions & 1 deletion InterfaCSS Tests/TestData/styleSheetPropertyValues.css
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,18 @@
.font7 {
font: 'Times New Roman' 5;
}

.font8 {
font: 42;
}
.font9 {
font: system 42;
}
.font10 {
font: systemItalic 42;
}
.font11 {
font: systemBold 42;
}

.image1 {
image: image.png;
Expand Down
2 changes: 1 addition & 1 deletion InterfaCSS.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'InterfaCSS'
s.version = '1.5.4'
s.version = '1.5.5'
s.summary = 'The CSS-inspired styling and layout framework for iOS'
s.homepage = 'https://github.com/tolo/InterfaCSS'
s.license = 'MIT'
Expand Down
8 changes: 8 additions & 0 deletions InterfaCSS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
CC3C8F2D4D8632FDA33164BF /* ISSRemoteFont.m in Sources */ = {isa = PBXBuildFile; fileRef = CC3C856B166302EF1EB76386 /* ISSRemoteFont.m */; };
CC3C8F48B33DD128C49135AC /* UIDevice+ISSAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = CC3C830A8A2EB2F6FBD714E6 /* UIDevice+ISSAdditions.m */; };
CC3C8FB344DE3874099F99EE /* NSDictionary+ISSDictionaryAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = CC3C8BC84D845AE136ED5484 /* NSDictionary+ISSDictionaryAdditions.h */; };
F654218C2220A09200699D64 /* NSArray+ISSAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = F654218A2220A09100699D64 /* NSArray+ISSAdditions.m */; };
F654218D2220A09200699D64 /* NSArray+ISSAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = F654218B2220A09100699D64 /* NSArray+ISSAdditions.h */; };
F659182B1C44190800F0836E /* ISSParser.h in Headers */ = {isa = PBXBuildFile; fileRef = F65918291C44190800F0836E /* ISSParser.h */; };
F659182C1C44190800F0836E /* ISSParser.m in Sources */ = {isa = PBXBuildFile; fileRef = F659182A1C44190800F0836E /* ISSParser.m */; };
F65D52D914F7B0F10034E496 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F65D52D814F7B0F10034E496 /* UIKit.framework */; };
Expand Down Expand Up @@ -206,6 +208,8 @@
CC3C8F35FA06B51A007B098C /* ISSRefreshableResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ISSRefreshableResource.h; sourceTree = "<group>"; };
CC3C8FD18CAA89A2BD0F5F5D /* UICollectionView+InterfaCSS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UICollectionView+InterfaCSS.m"; sourceTree = "<group>"; };
CC3C8FD30877A9A28290D23B /* UICollectionView+InterfaCSS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UICollectionView+InterfaCSS.h"; sourceTree = "<group>"; };
F654218A2220A09100699D64 /* NSArray+ISSAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+ISSAdditions.m"; sourceTree = "<group>"; };
F654218B2220A09100699D64 /* NSArray+ISSAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+ISSAdditions.h"; sourceTree = "<group>"; };
F65918291C44190800F0836E /* ISSParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ISSParser.h; sourceTree = "<group>"; };
F659182A1C44190800F0836E /* ISSParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ISSParser.m; sourceTree = "<group>"; };
F65D52D814F7B0F10034E496 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -388,6 +392,8 @@
CC3C8596A593D28BE30469C4 /* NSAttributedString+ISSAdditions.m */,
CC3C8BC84D845AE136ED5484 /* NSDictionary+ISSDictionaryAdditions.h */,
CC3C86B36E1E4AE2D2E09402 /* NSDictionary+ISSDictionaryAdditions.m */,
F654218B2220A09100699D64 /* NSArray+ISSAdditions.h */,
F654218A2220A09100699D64 /* NSArray+ISSAdditions.m */,
CC3C887D604FBF86926C2C7B /* NSMutableArray+ISSAdditions.h */,
CC3C8BBF4AE2866CC8BFF59F /* NSMutableArray+ISSAdditions.m */,
F6EBAD0E1768B0AA0053DAFA /* NSObject+ISSLogSupport.h */,
Expand Down Expand Up @@ -469,6 +475,7 @@
buildActionMask = 2147483647;
files = (
F6EBAD101768B0AA0053DAFA /* InterfaCSS.h in Headers */,
F654218D2220A09200699D64 /* NSArray+ISSAdditions.h in Headers */,
F6EBAD121768B0AA0053DAFA /* ISSSelector.h in Headers */,
F6EBAD141768B0AA0053DAFA /* ISSSelectorChain.h in Headers */,
F6EBAD161768B0AA0053DAFA /* ISSStyleSheet.h in Headers */,
Expand Down Expand Up @@ -626,6 +633,7 @@
F6EBAD1E1768B0AA0053DAFA /* UIView+InterfaCSS.m in Sources */,
F6EBAD221768B0AA0053DAFA /* NSObject+ISSLogSupport.m in Sources */,
27530C39182AF066000FED8A /* ISSPropertyDeclarations.m in Sources */,
F654218C2220A09200699D64 /* NSArray+ISSAdditions.m in Sources */,
27D8145017FB595800E365B1 /* NSString+ISSStringAdditions.m in Sources */,
F6EBAD68176B4A240053DAFA /* ISSDefaultStyleSheetParser.m in Sources */,
8AEE29831B716F26006991D5 /* ISSNestedElementSelector.m in Sources */,
Expand Down
20 changes: 16 additions & 4 deletions InterfaCSS/Parser/ISSDefaultStyleSheetParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "ISSPropertyDeclaration.h"
#import "NSObject+ISSLogSupport.h"
#import "NSString+ISSStringAdditions.h"
#import "NSArray+ISSAdditions.h"
#import "UIColor+ISSColorAdditions.h"
#import "ISSRectValue.h"
#import "ISSPointValue.h"
Expand Down Expand Up @@ -1152,14 +1153,16 @@ - (ISSParser*) propertyParsers:(ISSParser*)selectorsChainsDeclarations commentPa
// Ex: smaller(@font, 1)
// Ex: fontWithSize(@font, 12)
ISSParser* commaOrSpace = [[ISSParser choice:@[[ISSParser space], comma]] many1];
ISSParser* stringValue = [ISSParser choice:@[quotedString, anyName]];
ISSParser* remoteFontValueURLParser = [[ISSParser choice:@[quotedString, anyName]] transform:^id(id input) {
return [NSURL URLWithString:[input iss_trimQuotes]];
} name:@"remoteFontValueURLParser"];
ISSParser* remoteFontValueParser = [ISSParser iss_singleParameterFunctionParserWithName:@"url" parameterParser:remoteFontValueURLParser];
ISSParser* fontValueParser = [ISSParser choice:@[remoteFontValueParser, quotedString, anyName]];
ISSParser* localFontParser = [stringValue then:[ISSParser optional:[ISSParser sequential:@[commaOrSpace, stringValue]]]];
ISSParser* fontValueParser = [ISSParser choice:@[remoteFontValueParser, localFontParser]];

fontValueParser = [[fontValueParser keepLeft:commaOrSpace] then:fontValueParser];
fontValueParser = [fontValueParser transform:^id(NSArray* values) {
values = [values iss_flattened];
#if TARGET_OS_TV == 0
CGFloat fontSize = [UIFont systemFontSize];
#else
Expand Down Expand Up @@ -1197,8 +1200,17 @@ - (ISSParser*) propertyParsers:(ISSParser*)selectorsChainsDeclarations commentPa
}

if( remoteFontURL ) return [ISSRemoteFont remoteFontWithURL:remoteFontURL fontSize:fontSize];
else if( fontName ) return [UIFont fontWithName:fontName size:fontSize];
else return [UIFont systemFontOfSize:fontSize];
else {
if( [[fontName lowercaseString] hasPrefix:@"boldsystem"] || [[fontName lowercaseString] hasPrefix:@"systembold"] ) {
return [UIFont boldSystemFontOfSize:fontSize];
} else if( [[fontName lowercaseString] hasPrefix:@"italicsystem"] || [[fontName lowercaseString] hasPrefix:@"systemitalic"] ) {
return [UIFont italicSystemFontOfSize:fontSize];
} else if( !fontName || [fontName iss_isEqualIgnoreCase:@"system"] ) {
return [UIFont systemFontOfSize:fontSize];
} else {
return [UIFont fontWithName:fontName size:fontSize];
}
}
} name:@"font"];

ISSParser* fontFunctionParser = [[ISSParser sequential:@[identifier, [ISSParser unichar:'(' skipSpaces:YES],
Expand Down
19 changes: 19 additions & 0 deletions InterfaCSS/Util/NSArray+ISSAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// NSArray+ISSAdditions.h
// Part of InterfaCSS - http://www.github.com/tolo/InterfaCSS
//
// Copyright (c) Tobias Löfstrand, Leafnode AB.
// License: MIT - http://www.github.com/tolo/InterfaCSS/blob/master/LICENSE
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSArray (ISSAdditions)

- (NSArray*) iss_flattened;

@end

NS_ASSUME_NONNULL_END
32 changes: 32 additions & 0 deletions InterfaCSS/Util/NSArray+ISSAdditions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// NSArray+ISSAdditions.m
// Part of InterfaCSS - http://www.github.com/tolo/InterfaCSS
//
// Copyright (c) Tobias Löfstrand, Leafnode AB.
// License: MIT - http://www.github.com/tolo/InterfaCSS/blob/master/LICENSE
//

#import "NSArray+ISSAdditions.h"

#import "NSString+ISSStringAdditions.h"

static NSArray* flattenArray(NSArray* array) {
NSMutableArray* flattened = [NSMutableArray array];
for(id e in array) {
if( [e isKindOfClass:NSArray.class] ) {
[flattened addObjectsFromArray:flattenArray(e)];
} else if( e != [NSNull null] ) {
[flattened addObject:e];
}
}
return [flattened copy];
}


@implementation NSArray (ISSAdditions)

- (NSArray*) iss_flattened {
return flattenArray(self);
}

@end

0 comments on commit 1b6b18a

Please sign in to comment.