Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed problem with text box padding

  • Loading branch information...
commit edd03f76eebf63cdc8e5a9926e52582bd03af08c 1 parent 41ede70
@Cocoanetics authored
View
10 Core/Source/DTCoreTextLayoutFrame.m
@@ -236,6 +236,7 @@ - (void)_buildLinesWithTypesetter
CGFloat maxLineHeight = 0;
BOOL usesSyntheticLeading = NO;
+ BOOL usesForcedLineHeight = NO;
if (currentLineMetrics.leading == 0.0f)
{
@@ -261,6 +262,7 @@ - (void)_buildLinesWithTypesetter
{
if (lineHeight<minLineHeight)
{
+ usesForcedLineHeight = YES;
lineHeight = minLineHeight;
}
}
@@ -322,6 +324,7 @@ - (void)_buildLinesWithTypesetter
{
if (maxLineHeight>0 && lineHeight>maxLineHeight)
{
+ usesForcedLineHeight = YES;
lineHeight = maxLineHeight;
}
}
@@ -413,6 +416,13 @@ - (void)_buildLinesWithTypesetter
lineRange.location += lineRange.length;
+ // if there is a custom line height we need to adjust the ascender too
+ if (usesForcedLineHeight)
+ {
+ // causes the line frame to encompass also the extra space
+ newLine.ascent = lineHeight;
+ }
+
previousLine = newLine;
//previousLineMetrics = currentLineMetrics;
}
View
22 Core/Source/DTCoreTextLayoutLine.m
@@ -25,7 +25,7 @@ @implementation DTCoreTextLayoutLine
CGPoint _baselineOrigin;
- CGFloat ascent;
+ CGFloat _ascent;
CGFloat descent;
CGFloat leading;
CGFloat width;
@@ -271,7 +271,7 @@ - (void)_calculateMetrics
dispatch_sync(_syncQueue, ^{
if (!_didCalculateMetrics)
{
- width = (CGFloat)CTLineGetTypographicBounds(_line, &ascent, &descent, &leading);
+ width = (CGFloat)CTLineGetTypographicBounds(_line, &_ascent, &descent, &leading);
trailingWhitespaceWidth = (CGFloat)CTLineGetTrailingWhitespaceWidth(_line);
_didCalculateMetrics = YES;
@@ -365,7 +365,7 @@ - (CGRect)frame
[self _calculateMetrics];
}
- return CGRectMake(_baselineOrigin.x, _baselineOrigin.y - ascent, width, ascent + descent);
+ return CGRectMake(_baselineOrigin.x, _baselineOrigin.y - _ascent, width, _ascent + descent);
}
- (CGFloat)width
@@ -385,9 +385,21 @@ - (CGFloat)ascent
[self _calculateMetrics];
}
- return ascent;
+ return _ascent;
}
+- (void)setAscent:(CGFloat)ascent
+{
+ // need to get metrics because otherwise ascent gets overwritten
+ if (!_didCalculateMetrics)
+ {
+ [self _calculateMetrics];
+ }
+
+ _ascent = ascent;
+}
+
+
- (CGFloat)descent
{
if (!_didCalculateMetrics)
@@ -422,7 +434,7 @@ - (CGFloat)trailingWhitespaceWidth
@synthesize frame =_frame;
@synthesize glyphRuns = _glyphRuns;
-@synthesize ascent;
+@synthesize ascent = _ascent;
@synthesize descent;
@synthesize leading;
@synthesize trailingWhitespaceWidth;
View
3  Core/Source/DTHTMLElement.m
@@ -674,8 +674,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
if (webkitPaddingStart)
{
- padding.left = [webkitPaddingStart pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
- self.paragraphStyle.listIndent = padding.left;
+ self.paragraphStyle.listIndent = [webkitPaddingStart pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
}
BOOL needsTextBlock = (backgroundColor!=nil);
View
9 Core/Source/NSAttributedString+DTCoreText.m
@@ -812,6 +812,14 @@ + (NSAttributedString *)prefixForListItemWithCounter:(NSUInteger)listCounter lis
[newAttributes setObject:CFBridgingRelease(newParagraphStyle) forKey:(id)kCTParagraphStyleAttributeName];
}
+ // add textBlock if there's one (this has padding and background color)
+ NSArray *textBlocks = [attributes objectForKey:DTTextBlocksAttribute];
+ if (textBlocks)
+ {
+ [newAttributes setObject:textBlocks forKey:DTTextBlocksAttribute];
+ }
+
+ // transfer list style to new attributes
if (listStyle)
{
[newAttributes setObject:[NSArray arrayWithObject:listStyle] forKey:DTTextListsAttribute];
@@ -841,7 +849,6 @@ + (NSAttributedString *)prefixForListItemWithCounter:(NSUInteger)listCounter lis
NSMutableAttributedString *tmpStr = [[NSMutableAttributedString alloc] initWithString:prefix attributes:newAttributes];
-
if (image)
{
// make an attachment for the image
View
6 Demo/Resources/TextBoxes.html
@@ -1,7 +1,13 @@
<h3>Text Boxes</h3>
<p>Block-Level tags (like P or DIV) can have a padding and background color.</p>
+<p>Note: Only one level of boxing is properly supported. This is a crude workaround as CoreText does not support NSTextBlock on iOS.</p>
<pre>&lt;p style="background-color:yellow;padding:20px;"&gt;...&lt;/p&gt;</pre>
<p>By default the background is rectangle drawn for the entire width of the content view. There is a delegate method which you can override to do your own drawing. For example a stretchable UIImage. Or a rounded rectangle as shown here.</p>
<p style="background-color:yellow;padding: 20px;">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
+<h3>Some test cases</h3>
+<p style="background-color:#aabbcc;padding:10 10 10 10;">Before the list with 10px padding</p>
+<ul style="background-color:#cc2233;padding:30 30 30 30;">
+ <li style="line-height:50px;">These items</li><li style="line-height:50px;">have a fixed line height of 50px</li><li style="line-height:50px;">Note: The line-height becomes the ascender. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</li></ul>
+<p style="background-color:#aabbcc;padding:25 25 25 25;font-size:50px">After the list with 25 px padding</p>
Please sign in to comment.
Something went wrong with that request. Please try again.