Skip to content

Instantly share code, notes, and snippets.

@almostintuitive
Created June 8, 2014 15:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almostintuitive/2d8d4020d64d72db6789 to your computer and use it in GitHub Desktop.
Save almostintuitive/2d8d4020d64d72db6789 to your computer and use it in GitHub Desktop.
SVGView for iOS (using UIWebView)
//
// SVGView.h
// Just call the loadSVG method with the SVG's NSData to display it with transparent background & scrollbars.
//
// github.com/itchingpixels.
//
#import <UIKit/UIKit.h>
@interface SVGView : UIWebView <UIWebViewDelegate>
- (void)loadSVG: (NSData*)svgData;
@end
//
// SVGView.,
// Just call the loadSVG method with the SVG's NSData to display it with transparent background & scrollbars.
//
// github.com/itchingpixels.
//
#import "SVGView.h"
@implementation SVGView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)loadSVG:(NSData*)svgData {
[self setBackgroundColor:[UIColor clearColor]];
[self setOpaque:NO];
[[self scrollView] setBounces: NO];
self.delegate = self;
self.scrollView.scrollEnabled = NO;
[self loadData:svgData MIMEType:@"image/svg+xml" textEncodingName:0 baseURL:[[NSBundle mainBundle] bundleURL]];
self.contentMode = UIViewContentModeScaleAspectFill;
[self setNeedsDisplay];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGSize contentSize = webView.scrollView.contentSize;
CGSize viewSize = webView.bounds.size;
float rw = viewSize.width / contentSize.width;
self.scrollView.minimumZoomScale = rw;
self.scrollView.maximumZoomScale = rw;
self.scrollView.zoomScale = 1;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment