Skip to content

Commit

Permalink
Adding Image Support in iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlaerte committed Jul 18, 2017
1 parent 41e2081 commit 620a639
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 12 deletions.
2 changes: 2 additions & 0 deletions platform/ios/Bypass/Bypass.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
2305B2491F1D5146000A40D6 /* BPImageGetter.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BPImageGetter.h; sourceTree = "<group>"; };
58D9D454C9F2BD2E7F81B4B8 /* BPDisplaySettings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BPDisplaySettings.h; sourceTree = "<group>"; };
58D9DE2F0735C1CEB3500EFF /* BPDisplaySettings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BPDisplaySettings.m; sourceTree = "<group>"; };
5E007AE916FA17D100F8CFFD /* BPMarkdownView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BPMarkdownView.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -236,6 +237,7 @@
5E5BC87C16E1222A00165503 /* BPElementPrivate.h */,
58D9DE2F0735C1CEB3500EFF /* BPDisplaySettings.m */,
58D9D454C9F2BD2E7F81B4B8 /* BPDisplaySettings.h */,
2305B2491F1D5146000A40D6 /* BPImageGetter.h */,
);
name = Model;
sourceTree = "<group>";
Expand Down
3 changes: 3 additions & 0 deletions platform/ios/Bypass/Bypass/BPAttributedStringConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#import <UIKit/UIKit.h>
#import "BPDocument.h"
#import "BPImageGetter.h"

@class BPDisplaySettings;

Expand All @@ -34,4 +35,6 @@ OBJC_EXPORT NSString *const BPLinkStyleAttributeName;

- (NSAttributedString *)convertDocument:(BPDocument *)document;

- (NSAttributedString *)convertDocument:(BPDocument *)document WithImageGetter: (id<BPImageGetter>)imageGetter;

@end
59 changes: 47 additions & 12 deletions platform/ios/Bypass/Bypass/BPAttributedStringConverter.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import <CoreText/CoreText.h>
#import "BPAttributedStringConverter.h"
#import "BPDisplaySettings.h"
#import "BPImageGetter.h"

NSString *const BPLinkStyleAttributeName = @"NSLinkAttributeName";

Expand All @@ -44,19 +45,25 @@ - (id)init {

- (NSAttributedString *)convertDocument:(BPDocument *)document
{
NSMutableAttributedString *target = [[NSMutableAttributedString alloc] init];

for (BPElement *element in [document elements]) {
[self convertElement:element toTarget:target];
}

[target addAttribute:NSForegroundColorAttributeName value:[_displaySettings defaultColor] range:NSMakeRange(0, target.length)];

return target;
return [self convertDocument:document WithImageGetter:nil];
}

- (NSAttributedString *)convertDocument:(BPDocument *)document
WithImageGetter:(id<BPImageGetter>)imageGetter
{
NSMutableAttributedString *target = [[NSMutableAttributedString alloc] init];

for (BPElement *element in [document elements]) {
[self convertElement:element WithImageGetter:imageGetter toTarget:target];
}

[target addAttribute:NSForegroundColorAttributeName value:[_displaySettings defaultColor] range:NSMakeRange(0, target.length)];

return target;
}

- (void)convertElement:(BPElement *)element toTarget:(NSMutableAttributedString *)target
- (void)convertElement:(BPElement *)element WithImageGetter:(id<BPImageGetter>)imageGetter
toTarget:(NSMutableAttributedString *)target
{
// Capture the starting point of the effective range to apply attributes to

Expand All @@ -81,7 +88,7 @@ - (void)convertElement:(BPElement *)element toTarget:(NSMutableAttributedString
} else if (elementType == BPEmphasis) {
[self renderItalicElement:element toTarget:target];
} else if (elementType == BPImage) {
// Currently not supported
[self renderImageElement:element WithImageGetter:imageGetter toTarget:target];
} else if (elementType == BPLineBreak) {
[self renderLineBreak:element toTarget:target];
} else if (elementType == BPLink) {
Expand All @@ -99,7 +106,7 @@ - (void)convertElement:(BPElement *)element toTarget:(NSMutableAttributedString
// Render children of this particular element recursively

for (BPElement *childElement in [element childElements]) {
[self convertElement:childElement toTarget:target];
[self convertElement:childElement WithImageGetter:imageGetter toTarget:target];
}

// Capture the end of the range
Expand Down Expand Up @@ -240,6 +247,34 @@ - (void)renderLinkElement:(BPElement *)element
toTarget:target];
}

- (void)renderImageElement:(BPElement *)element
WithImageGetter:(id<BPImageGetter>) imageGetter
toTarget:(NSMutableAttributedString *)target
{
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSFontAttributeName] = [_displaySettings defaultFont];
attributes[NSForegroundColorAttributeName] = [_displaySettings defaultColor];

NSString *alt = [[element attributes] objectForKey:@"alt"];
alt = (alt == nil) ? @"" : alt;

NSString *link = [[element attributes] objectForKey:@"link"];

if (imageGetter != nil) {
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [imageGetter getUIImageWithSource:link];

NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[target appendAttributedString:attrStringWithImage];

} else {
NSString *text = [[NSString alloc] initWithFormat:@"![%@](%@)", alt, link];
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text
attributes:attributes];
[target appendAttributedString:attributedText];
}
}

- (void)renderLineBreak:(BPElement *)element
toTarget:(NSMutableAttributedString *)target
{
Expand Down
32 changes: 32 additions & 0 deletions platform/ios/Bypass/Bypass/BPImageGetter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// BPImageGetter.h
// Bypass
//
// Created by Victor Oliveira on 17/07/17.
// Copyright © 2017 Uncodin. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <UIKit/UIKit.h>
#ifndef BPImageGetter_h
#define BPImageGetter_h

@protocol BPImageGetter

@required
- (UIImage *) getUIImageWithSource:(NSString *)source;

@end

#endif /* BPImageGetter_h */

0 comments on commit 620a639

Please sign in to comment.