Permalink
Browse files

[css] added padding support to supplement auto sizing

  • Loading branch information...
djMax committed Mar 5, 2013
1 parent 36f7c84 commit fdaf94b696d9b0479681cc6cb94904ca6a724008
@@ -145,7 +145,7 @@
66832DEF14434FBE003E413C /* UILabel+NIStyleable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UILabel+NIStyleable.h"; path = "../../src/css/src/UILabel+NIStyleable.h"; sourceTree = "<group>"; };
66832DF014434FBE003E413C /* UILabel+NIStyleable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UILabel+NIStyleable.m"; path = "../../src/css/src/UILabel+NIStyleable.m"; sourceTree = "<group>"; };
66832DF114434FBE003E413C /* UIView+NIStyleable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIView+NIStyleable.h"; path = "../../src/css/src/UIView+NIStyleable.h"; sourceTree = "<group>"; };
- 66832DF214434FBE003E413C /* UIView+NIStyleable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIView+NIStyleable.m"; path = "../../src/css/src/UIView+NIStyleable.m"; sourceTree = "<group>"; };
+ 66832DF214434FBE003E413C /* UIView+NIStyleable.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.c.objc; name = "UIView+NIStyleable.m"; path = "../../src/css/src/UIView+NIStyleable.m"; sourceTree = "<group>"; tabWidth = 2; };
66832DF314434FBE003E413C /* UINavigationBar+NIStyleable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UINavigationBar+NIStyleable.h"; path = "../../src/css/src/UINavigationBar+NIStyleable.h"; sourceTree = "<group>"; };
66832DF414434FBE003E413C /* UINavigationBar+NIStyleable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UINavigationBar+NIStyleable.m"; path = "../../src/css/src/UINavigationBar+NIStyleable.m"; sourceTree = "<group>"; };
66832DF514434FBE003E413C /* NICSSParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NICSSParser.h; path = ../../src/css/src/NICSSParser.h; sourceTree = "<group>"; };
View
@@ -93,6 +93,8 @@ typedef enum {
NICSSUnit _marginLeft;
NICSSUnit _marginRight;
NICSSUnit _marginBottom;
+ NICSSUnit _verticalPadding;
+ NICSSUnit _horizontalPadding;
NICSSUnit _width;
NICSSUnit _height;
@@ -122,6 +124,7 @@ typedef enum {
int BackgroundColor : 1;
int BackgroundImage: 1;
int BackgroundStretchInsets: 1;
+ //16
int Image: 1;
int BorderRadius : 1;
int BorderColor : 1;
@@ -138,6 +141,7 @@ typedef enum {
int Top : 1;
int Bottom : 1;
int Left : 1;
+ // 32
int Right : 1;
int FrameHorizontalAlign: 1;
int FrameVerticalAlign: 1;
@@ -154,8 +158,11 @@ typedef enum {
int MinHeight: 1;
int MaxWidth: 1;
int MaxHeight: 1;
+ // 48
int TextKey: 1;
int ButtonAdjust: 1;
+ int HorizontalPadding: 1;
+ int VerticalPadding: 1;
} cached;
int64_t _data;
} _is;
@@ -311,6 +318,11 @@ typedef enum {
- (BOOL)hasTextKey;
- (NSString*)textKey; // -mobile-text-key
+- (BOOL) hasHorizontalPadding;
+- (NICSSUnit) horizontalPadding; // padding or -mobile-hPadding
+
+- (BOOL) hasVerticalPadding;
+- (NICSSUnit) verticalPadding; // padding or -mobile-vPadding
@end
/**
@@ -593,3 +605,15 @@ typedef enum {
*
* @fn NICSSRuleset::cssRuleForKey
*/
+
+/**
+ * For views that support sizeToFit, padding will add a value to the computed size
+ *
+ * @fn NICSSRuleset::horizontalPadding
+ */
+
+/**
+ * For views that support sizeToFit, padding will add a value to the computed size
+ *
+ * @fn NICSSRuleset::verticalPadding
+ */
View
@@ -50,6 +50,9 @@
static NSString* const kAutoresizingKey = @"-ios-autoresizing";
static NSString* const kTableViewCellSeparatorStyleKey = @"-ios-table-view-cell-separator-style";
static NSString* const kScrollViewIndicatorStyleKey = @"-ios-scroll-view-indicator-style";
+static NSString* const kPaddingKey = @"padding";
+static NSString* const kHPaddingKey = @"-mobile-hpadding";
+static NSString* const kVPaddingKey = @"-mobile-vpadding";
// This color table is generated on-demand and is released when a memory warning is encountered.
static NSDictionary* sColorTable = nil;
@@ -178,6 +181,56 @@ - (UITextAlignment)textAlignment {
return _textAlignment;
}
+///////////////////////////////////////////////////////////////////////////////////////////////////
+-(BOOL)hasHorizontalPadding {
+ return nil != [_ruleset objectForKey:kPaddingKey] || nil != [_ruleset objectForKey:kHPaddingKey];
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+-(NICSSUnit)horizontalPadding {
+ NIDASSERT([self hasHorizontalPadding]);
+ NSArray *css = [_ruleset objectForKey:kHPaddingKey];
+ if (css && css.count > 0) {
+ return [NICSSRuleset unitFromCssValues:css];
+ }
+ css = [_ruleset objectForKey:kPaddingKey];
+ if (css && css.count > 1) {
+ return [NICSSRuleset unitFromCssValues:css offset:1];
+ }
+ if (css && css.count == 1) {
+ return [NICSSRuleset unitFromCssValues:css];
+ } else {
+ NICSSUnit unit;
+ NIDASSERT([css count] > 0);
+ unit.type = CSS_PIXEL_UNIT;
+ unit.value = 0;
+ return unit;
+ }
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+-(BOOL)hasVerticalPadding {
+ return nil != [_ruleset objectForKey:kPaddingKey] || nil != [_ruleset objectForKey:kVPaddingKey];
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+-(NICSSUnit)verticalPadding {
+ NIDASSERT([self hasVerticalPadding]);
+ NSArray *css = [_ruleset objectForKey:kVPaddingKey];
+ if (css && css.count > 0) {
+ return [NICSSRuleset unitFromCssValues:css];
+ }
+ css = [_ruleset objectForKey:kPaddingKey];
+ if (css && css.count > 0) {
+ return [NICSSRuleset unitFromCssValues:css];
+ } else {
+ NICSSUnit unit;
+ NIDASSERT([css count] > 0);
+ unit.type = CSS_PIXEL_UNIT;
+ unit.value = 0;
+ return unit;
+ }
+}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)hasFont {
@@ -1006,8 +1059,12 @@ + (NSDictionary *)colorTable {
///////////////////////////////////////////////////////////////////////////////////////////////////
+ (NICSSUnit)unitFromCssValues:(NSArray*)cssValues {
+ return [self unitFromCssValues:cssValues offset: 0];
+}
+
++ (NICSSUnit)unitFromCssValues:(NSArray*)cssValues offset: (int) offset {
NICSSUnit returnUnits;
- NSString *unitValue = [cssValues objectAtIndex:0];
+ NSString *unitValue = [cssValues objectAtIndex:offset];
if ([unitValue caseInsensitiveCompare:@"auto"] == NSOrderedSame) {
returnUnits.type = CSS_AUTO_UNIT;
returnUnits.value = 0;
@@ -152,6 +152,49 @@ - (NSString*)applyOrDescribe: (BOOL) apply ruleSet: (NICSSRuleset*) ruleSet inDO
} else {
[desc appendFormat:@"[%@ sizeToFit];\n", name];
}
+ if (ruleSet.hasVerticalPadding) {
+ NICSSUnit vPadding = ruleSet.verticalPadding;
+ switch (vPadding.type) {
+ case CSS_AUTO_UNIT:
+ break;
+ case CSS_PERCENTAGE_UNIT:
+ if (apply) {
+ self.frameHeight += (self.frameHeight * vPadding.value);
+ } else {
+ [desc appendFormat:@"%@.frameHeight += (%@.frameHeight * %f);", name, name, vPadding.value];
+ }
+ break;
+ case CSS_PIXEL_UNIT:
+ if (apply) {
+ self.frameHeight += vPadding.value;
+ } else {
+ [desc appendFormat:@"%@.frameHeight += %f;", name, vPadding.value];
+ }
+ break;
+ }
+ }
+ if (ruleSet.hasHorizontalPadding) {
+ NICSSUnit hPadding = ruleSet.horizontalPadding;
+ switch (hPadding.type) {
+ case CSS_AUTO_UNIT:
+ break;
+ case CSS_PERCENTAGE_UNIT:
+ if (apply) {
+ self.frameWidth += (self.frameWidth * hPadding.value);
+ } else {
+ [desc appendFormat:@"%@.frameWidth += (%@.frameWidth * %f);", name, name, hPadding.value];
+ }
+ break;
+ case CSS_PIXEL_UNIT:
+ if (apply) {
+ self.frameWidth += hPadding.value;
+ } else {
+ [desc appendFormat:@"%@.frameWidth += %f;", name, hPadding.value];
+ }
+ break;
+ }
+ }
+
} else {
if ([ruleSet hasWidth]) {
NICSSUnit u = ruleSet.width;
@@ -192,6 +235,27 @@ - (NSString*)applyOrDescribe: (BOOL) apply ruleSet: (NICSSRuleset*) ruleSet inDO
}
break;
}
+ if (ruleSet.hasHorizontalPadding) {
+ NICSSUnit hPadding = ruleSet.horizontalPadding;
+ switch (hPadding.type) {
+ case CSS_AUTO_UNIT:
+ break;
+ case CSS_PERCENTAGE_UNIT:
+ if (apply) {
+ self.frameWidth += (self.frameWidth * hPadding.value);
+ } else {
+ [desc appendFormat:@"%@.frameWidth += (%@.frameWidth * %f);", name, name, hPadding.value];
+ }
+ break;
+ case CSS_PIXEL_UNIT:
+ if (apply) {
+ self.frameWidth += hPadding.value;
+ } else {
+ [desc appendFormat:@"%@.frameWidth += %f;", name, hPadding.value];
+ }
+ break;
+ }
+ }
}
if ([ruleSet hasHeight]) {
NICSSUnit u = ruleSet.height;
@@ -232,6 +296,27 @@ - (NSString*)applyOrDescribe: (BOOL) apply ruleSet: (NICSSRuleset*) ruleSet inDO
}
break;
}
+ if (ruleSet.hasVerticalPadding) {
+ NICSSUnit vPadding = ruleSet.verticalPadding;
+ switch (vPadding.type) {
+ case CSS_AUTO_UNIT:
+ break;
+ case CSS_PERCENTAGE_UNIT:
+ if (apply) {
+ self.frameHeight += (self.frameHeight * vPadding.value);
+ } else {
+ [desc appendFormat:@"%@.frameHeight += (%@.frameHeight * %f);", name, name, vPadding.value];
+ }
+ break;
+ case CSS_PIXEL_UNIT:
+ if (apply) {
+ self.frameHeight += vPadding.value;
+ } else {
+ [desc appendFormat:@"%@.frameHeight += %f;", name, vPadding.value];
+ }
+ break;
+ }
+ }
}
}

0 comments on commit fdaf94b

Please sign in to comment.