-
Notifications
You must be signed in to change notification settings - Fork 104
[iOS & Android] Enable borderRadius in all mention types #720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
0986812
195ad24
1b26ad4
521e78c
9d04008
5a7c73c
ddeba51
ac410ee
870b5da
99cf1fb
8b416a5
28b6a13
98dafda
a105c10
db307b2
8d5e9b2
bd8aaf3
2a5069e
cbab252
f1b8c74
46ad23f
fe24512
39e0318
0da8207
48830e5
6665f83
c556257
eb521b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| #import <RNLiveMarkdown/MarkdownTextLayoutFragment.h> | ||
| #import <RNLiveMarkdown/MarkdownFormatter.h> | ||
|
|
||
| @implementation MarkdownTextLayoutFragment | ||
|
|
||
| #pragma mark - overriding class methods | ||
|
|
||
| - (CGRect)renderingSurfaceBounds { | ||
| if (self.depth == nil) { | ||
| return [super renderingSurfaceBounds]; | ||
| } | ||
| return CGRectUnion(self.boundingRect, [super renderingSurfaceBounds]); | ||
| } | ||
|
|
||
| - (void)drawAtPoint:(CGPoint)point inContext:(CGContextRef)ctx { | ||
| if (self.textLineFragments.count == 0) { | ||
| [super drawAtPoint:point inContext:ctx]; | ||
| return; | ||
| } | ||
|
|
||
| [self drawRibbon]; | ||
|
war-in marked this conversation as resolved.
Outdated
|
||
| [self drawMentions]; | ||
|
|
||
| [super drawAtPoint:point inContext:ctx]; | ||
| } | ||
|
|
||
| #pragma mark - drawing custom elements | ||
|
|
||
| - (void)drawRibbon { | ||
| if (self.depth == nil) { | ||
| return; | ||
| } | ||
|
|
||
| CGFloat marginLeft = _markdownUtils.markdownStyle.blockquoteMarginLeft; | ||
| CGFloat borderWidth = _markdownUtils.markdownStyle.blockquoteBorderWidth; | ||
| CGFloat paddingLeft = _markdownUtils.markdownStyle.blockquotePaddingLeft; | ||
| CGFloat shift = marginLeft + borderWidth + paddingLeft; | ||
|
|
||
| [_markdownUtils.markdownStyle.blockquoteBorderColor setFill]; | ||
|
|
||
| CGRect boundingRect = self.boundingRect; | ||
| for (NSUInteger i = 0; i < [_depth unsignedIntValue]; ++i) { | ||
| CGRect ribbonRect = CGRectMake(boundingRect.origin.x + i * shift, boundingRect.origin.y, borderWidth, boundingRect.size.height); | ||
|
war-in marked this conversation as resolved.
Outdated
|
||
| UIRectFill(ribbonRect); | ||
| } | ||
| } | ||
|
|
||
| - (void)drawMentions { | ||
| NSMutableArray<NSDictionary *> *mentions = [self getMentions]; | ||
|
war-in marked this conversation as resolved.
Outdated
|
||
|
|
||
| [self.textLineFragments enumerateObjectsUsingBlock:^(NSTextLineFragment * _Nonnull lineFragment, NSUInteger idx, BOOL * _Nonnull stop) { | ||
| if (lineFragment.characterRange.length == 0) { | ||
| return; | ||
| } | ||
|
|
||
| CGRect lineBounds = lineFragment.typographicBounds; | ||
| for (NSDictionary *mention in mentions) { | ||
| NSRange mentionRange = [mention[@"range"] rangeValue]; | ||
| UIColor *backgroundColor = mention[@"value"][@"backgroundColor"]; | ||
| CGFloat cornerRadius = [mention[@"value"][@"cornerRadius"] floatValue]; | ||
|
war-in marked this conversation as resolved.
Outdated
|
||
|
|
||
| NSRange intersection = NSIntersectionRange(lineFragment.characterRange, mentionRange); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to use
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, both |
||
| if (intersection.length == 0) { | ||
| continue; | ||
| } | ||
|
|
||
| BOOL isStart = (intersection.location == mentionRange.location); | ||
| BOOL isEnd = (NSMaxRange(intersection) == NSMaxRange(mentionRange)); | ||
|
|
||
| CGPoint startLocation = [lineFragment locationForCharacterAtIndex:intersection.location]; | ||
| CGPoint endLocation = [lineFragment locationForCharacterAtIndex:intersection.location + intersection.length]; | ||
|
|
||
| CGRect paddedRect = CGRectMake(startLocation.x, | ||
| lineBounds.origin.y, | ||
| endLocation.x - startLocation.x, | ||
|
war-in marked this conversation as resolved.
Outdated
|
||
| lineBounds.size.height); | ||
|
|
||
| UIRectCorner cornersToRound = 0; | ||
|
war-in marked this conversation as resolved.
|
||
| if (isStart && isEnd) { | ||
| cornersToRound = UIRectCornerAllCorners; | ||
| } else if (isStart) { | ||
| cornersToRound = (UIRectCornerTopLeft | UIRectCornerBottomLeft); | ||
| } else if (isEnd) { | ||
| cornersToRound = (UIRectCornerTopRight | UIRectCornerBottomRight); | ||
| } | ||
|
war-in marked this conversation as resolved.
Outdated
|
||
|
|
||
| UIBezierPath *linePath; | ||
| linePath = [UIBezierPath bezierPathWithRoundedRect:paddedRect | ||
| byRoundingCorners:cornersToRound | ||
| cornerRadii:CGSizeMake(cornerRadius, cornerRadius)]; | ||
|
war-in marked this conversation as resolved.
Outdated
|
||
|
|
||
| [backgroundColor setFill]; | ||
| [linePath fill]; | ||
| } | ||
| }]; | ||
| } | ||
|
|
||
| #pragma mark - helper functions | ||
|
|
||
| - (CGRect)boundingRect { | ||
| CGRect fragmentTextBounds = CGRectNull; | ||
| for (NSTextLineFragment *lineFragment in self.textLineFragments) { | ||
| if (lineFragment.characterRange.length == 0) { | ||
| continue; | ||
| } | ||
| CGRect lineFragmentBounds = lineFragment.typographicBounds; | ||
| if (CGRectIsNull(fragmentTextBounds)) { | ||
| fragmentTextBounds = lineFragmentBounds; | ||
|
war-in marked this conversation as resolved.
|
||
| } else { | ||
| fragmentTextBounds = CGRectUnion(fragmentTextBounds, lineFragmentBounds); | ||
| } | ||
| } | ||
|
|
||
| CGFloat marginLeft = _markdownUtils.markdownStyle.blockquoteMarginLeft; | ||
| CGFloat borderWidth = _markdownUtils.markdownStyle.blockquoteBorderWidth; | ||
| CGFloat paddingLeft = _markdownUtils.markdownStyle.blockquotePaddingLeft; | ||
| CGFloat shift = marginLeft + borderWidth + paddingLeft; | ||
|
|
||
| fragmentTextBounds.origin.x -= (paddingLeft + borderWidth) + shift * ([_depth unsignedIntValue] - 1); | ||
| fragmentTextBounds.size.width = borderWidth + shift * ([_depth unsignedIntValue] - 1); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the blockquote logic is now duplicated, can we somehow dedupe it?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was already :/ I think we need it to be that way because boundingRect is also used in renderingSurfaceBounds |
||
|
|
||
| return fragmentTextBounds; | ||
| } | ||
|
|
||
| - (NSMutableArray<NSDictionary *>*)getMentions { | ||
|
war-in marked this conversation as resolved.
Outdated
|
||
| NSTextParagraph *paragraph = (NSTextParagraph *)self.textElement; | ||
| NSAttributedString *attributedString = [paragraph attributedString]; | ||
|
|
||
| NSMutableArray<NSDictionary *> *mentions = [NSMutableArray array]; | ||
| [attributedString enumerateAttribute:RCTLiveMarkdownMentionAttributeName | ||
| inRange:NSMakeRange(0, attributedString.length) | ||
| options:0 | ||
| usingBlock:^(id value, NSRange range, BOOL *stop) { | ||
| if (value) { | ||
| [mentions addObject:@{ | ||
| @"range": [NSValue valueWithRange:range], | ||
| @"value": value | ||
| }]; | ||
|
war-in marked this conversation as resolved.
Outdated
|
||
| } | ||
| }]; | ||
|
|
||
| return mentions; | ||
| } | ||
|
|
||
| @end | ||
|
war-in marked this conversation as resolved.
|
|
war-in marked this conversation as resolved.
|
Uh oh!
There was an error while loading. Please reload this page.