Skip to content
Browse files

added support for border-color,border-radius, border-width

  • Loading branch information...
1 parent 8a28291 commit 0db7ed1caed5b9c436d50eb916198ffd78976b7e @farfromrefug farfromrefug committed Dec 6, 2013
View
3 Core/Source/DTCoreTextConstants.h
@@ -73,6 +73,9 @@ extern NSString * const DTTextBlocksAttribute;
extern NSString * const DTFieldAttribute;
extern NSString * const DTCustomAttributesAttribute;
extern NSString * const DTAscentMultiplierAttribute;
+extern NSString * const DTBackgroundStrokeColorAttribute;
+extern NSString * const DTBackgroundStrokeWidthAttribute;
+extern NSString * const DTBackgroundCornerRadiusAttribute;
// field constants
View
3 Core/Source/DTCoreTextConstants.m
@@ -45,6 +45,9 @@
NSString * const DTFieldAttribute = @"DTField";
NSString * const DTCustomAttributesAttribute = @"DTCustomAttributes";
NSString * const DTAscentMultiplierAttribute = @"DTAscentMultiplierAttribute";
+NSString * const DTBackgroundStrokeColorAttribute = @"DTBackgroundStrokeColor";
+NSString * const DTBackgroundStrokeWidthAttribute = @"DTBackgroundStrokeWidth";
+NSString * const DTBackgroundCornerRadiusAttribute = @"DTBackgroundCornerRadius";
// field constants
NSString * const DTListPrefixField = @"{listprefix}";
View
2 Core/Source/DTCoreTextFontDescriptor.m
@@ -117,7 +117,7 @@ + (void)_createDictionaryOfAllAvailableFontOverrideNamesWithCompletion:(void(^)(
// get all font descriptors
DTCoreTextFontCollection *allFonts = [DTCoreTextFontCollection availableFontsCollection];
- NSMutableDictionary *tmpDictionary = [NSMutableDictionary dictionaryWithCapacity:[allFonts.fontDescriptors count]];
+ NSMutableDictionary *tmpDictionary = [[NSMutableDictionary alloc] initWithCapacity:[allFonts.fontDescriptors count]];
// sort font descriptors by name so that shorter names are preferred
NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"fontFamily" ascending:YES];
View
44 Core/Source/DTHTMLElement.m
@@ -296,6 +296,21 @@ - (NSDictionary *)attributesForAttributedStringRepresentation
{
[tmpDict setObject:_paragraphStyle.textBlocks forKey:DTTextBlocksAttribute];
}
+
+ if (_backgroundStrokeColor)
+ {
+ [tmpDict setObject:(id)[_backgroundStrokeColor CGColor] forKey:DTBackgroundStrokeColorAttribute];
+ }
+
+ if (_backgroundStrokeWidth)
+ {
+ [tmpDict setObject:DTNSNumberFromCGFloat(_backgroundStrokeWidth) forKey:DTBackgroundStrokeWidthAttribute];
+ }
+
+ if (_backgroundCornerRadius)
+ {
+ [tmpDict setObject:DTNSNumberFromCGFloat(_backgroundCornerRadius) forKey:DTBackgroundCornerRadiusAttribute];
+ }
return tmpDict;
}
@@ -1166,7 +1181,29 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}
}
- BOOL needsTextBlock = (_backgroundColor!=nil);
+ NSString *borderColor = [styles objectForKey:@"border-color"];
+ if (borderColor)
+ {
+ self.backgroundStrokeColor = DTColorCreateWithHTMLName(borderColor);
+ }
+ NSString *borderWidth = [[styles objectForKey:@"border-width"] lowercaseString];
+ if (borderWidth)
+ {
+ _backgroundStrokeWidth = [borderWidth floatValue];
+ }
+ else {
+ _backgroundStrokeWidth = 0.0f;
+ }
+ NSString *cornerRadius = [[styles objectForKey:@"border-radius"] lowercaseString];
+ if (cornerRadius)
+ {
+ _backgroundCornerRadius = [cornerRadius floatValue];
+ }
+ else {
+ _backgroundCornerRadius = 0.0f;
+ }
+
+ BOOL needsTextBlock = (_backgroundColor!=nil || _backgroundStrokeColor!=nil || _backgroundCornerRadius > 0 || _backgroundStrokeWidth > 0);
BOOL hasMargins = NO;
@@ -1355,6 +1392,11 @@ - (void)inheritAttributesFromElement:(DTHTMLElement *)element
_currentTextSize = element.currentTextSize;
_textScale = element.textScale;
+ _backgroundColor = element.backgroundColor;
+ _backgroundStrokeColor = element.backgroundStrokeColor;
+ _backgroundStrokeWidth = element.backgroundStrokeWidth;
+ _backgroundCornerRadius = element.backgroundCornerRadius;
+
// only inherit background-color from inline elements
if (element.displayStyle == DTHTMLElementDisplayStyleInline || element.displayStyle == DTHTMLElementDisplayStyleListItem)
{
View
35 Core/Source/NSDictionary+DTCoreText.m
@@ -221,4 +221,39 @@ - (CGFloat)kerning
return [kerningNum floatValue];
}
+- (DTColor *)backgroundStrokeColor
+{
+ CGColorRef cgColor = (__bridge CGColorRef)[self objectForKey:DTBackgroundStrokeColorAttribute];
+
+ if (cgColor)
+ {
+ return [DTColor colorWithCGColor:cgColor];
+ }
+ return nil;
+}
+
+- (CGFloat)backgroundStrokeWidth
+{
+ NSNumber *num = [self objectForKey:DTBackgroundStrokeWidthAttribute];
+
+ if (num)
+ {
+ return [num floatValue];
+ }
+
+ return 0.0f;
+}
+
+- (CGFloat)backgroundCornerRadius
+{
+ NSNumber *num = [self objectForKey:DTBackgroundCornerRadiusAttribute];
+
+ if (num)
+ {
+ return [num floatValue];
+ }
+
+ return 0.0f;
+}
+
@end
View
2 Demo/Resources/About.html
@@ -1,6 +1,6 @@
<html>
<body>
- <div style="background-color:yellow; padding:5px">
+ <div style="border-width: 1px;border-color: yellow;background-color:blue; border-radius:25px;padding:5px">
<p>DTCoreText consists of two portions:</p>
<ol>
<li><b>Parsing HTML</b> and creating NSAttributedStrings</li>

0 comments on commit 0db7ed1

Please sign in to comment.
Something went wrong with that request. Please try again.