Skip to content

Commit

Permalink
Use UIGraphicsImageRenderer instead of UIGraphicsBeginImageContextWit…
Browse files Browse the repository at this point in the history
…hOptions (#54)
  • Loading branch information
wanhmr committed Aug 26, 2023
1 parent e1cbea7 commit 6829d96
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 56 deletions.
2 changes: 1 addition & 1 deletion MPITextKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'MPITextKit'
s.version = '0.2.1'
s.version = '0.2.2'
s.summary = 'Powerful text framework for iOS to display text based on TextKit.'

# This description is used to generate tags and improve search results.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) 
[![SPM supported](https://img.shields.io/badge/SPM-supported-DE5C43.svg?style=flat)](https://swift.org/package-manager) 
[![Platform](https://img.shields.io/cocoapods/p/MPITextKit.svg?style=flat)](http://cocoadocs.org/docsets/MPITextKit) 
[![Support](https://img.shields.io/badge/support-iOS%209%2B%20-blue.svg?style=flat)](https://www.apple.com/nl/ios/) 
[![Support](https://img.shields.io/badge/support-iOS%2010%2B%20-blue.svg?style=flat)](https://www.apple.com/nl/ios/) 

Powerful text framework for iOS to display rich text based on TextKit, inspired by [Texture](https://github.com/texturegroup/texture) and [YYText](https://github.com/ibireme/YYText).

Expand All @@ -27,14 +27,14 @@ Powerful text framework for iOS to display rich text based on TextKit, inspired
- High performance asynchronous text layout and rendering
- Text attachments with UIImage, UIView and CALayer
- Exclusion paths
- Custom attribute
- Custom attributes
- Custom truncation token
- Debug text layout
- Text selection

## Requirements

- iOS 9+
- iOS 10+

## Installation

Expand Down
103 changes: 51 additions & 52 deletions Sources/MPITextMagnifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,60 +87,59 @@ + (UIImage *)coverImage {
CGRect rect = (CGRect) {.size = size, .origin = CGPointZero};
rect = CGRectInset(rect, kPadding, kPadding);

UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

CGPathRef boxPath = CGPathCreateWithRect(CGRectMake(0, 0, size.width, size.height), NULL);
CGPathRef fillPath = CGPathCreateWithEllipseInRect(rect, NULL);
CGPathRef strokePath = CGPathCreateWithEllipseInRect(MPITextCGRectPixelHalf(rect), NULL);

// inner shadow
CGContextSaveGState(context); {
CGFloat blurRadius = 25;
CGSize offset = CGSizeMake(0, 15);
CGColorRef shadowColor = [UIColor colorWithWhite:0 alpha:0.16].CGColor;
CGColorRef opaqueShadowColor = CGColorCreateCopyWithAlpha(shadowColor, 1.0);
CGContextAddPath(context, fillPath);
CGContextClip(context);
CGContextSetAlpha(context, CGColorGetAlpha(shadowColor));
CGContextBeginTransparencyLayer(context, NULL); {
CGContextSetShadowWithColor(context, offset, blurRadius, opaqueShadowColor);
CGContextSetBlendMode(context, kCGBlendModeSourceOut);
CGContextSetFillColorWithColor(context, opaqueShadowColor);
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size format:format];
UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
CGContextRef context = rendererContext.CGContext;
CGPathRef boxPath = CGPathCreateWithRect(CGRectMake(0, 0, size.width, size.height), NULL);
CGPathRef fillPath = CGPathCreateWithEllipseInRect(rect, NULL);
CGPathRef strokePath = CGPathCreateWithEllipseInRect(MPITextCGRectPixelHalf(rect), NULL);

// inner shadow
CGContextSaveGState(context); {
CGFloat blurRadius = 25;
CGSize offset = CGSizeMake(0, 15);
CGColorRef shadowColor = [UIColor colorWithWhite:0 alpha:0.16].CGColor;
CGColorRef opaqueShadowColor = CGColorCreateCopyWithAlpha(shadowColor, 1.0);
CGContextAddPath(context, fillPath);
CGContextFillPath(context);
} CGContextEndTransparencyLayer(context);
CGColorRelease(opaqueShadowColor);
} CGContextRestoreGState(context);

// outer shadow
CGContextSaveGState(context); {
CGContextAddPath(context, boxPath);
CGContextAddPath(context, fillPath);
CGContextEOClip(context);
CGColorRef shadowColor = [UIColor colorWithWhite:0 alpha:0.32].CGColor;
CGContextSetShadowWithColor(context, CGSizeMake(0, 1.5), 3, shadowColor);
CGContextBeginTransparencyLayer(context, NULL); {
CGContextClip(context);
CGContextSetAlpha(context, CGColorGetAlpha(shadowColor));
CGContextBeginTransparencyLayer(context, NULL); {
CGContextSetShadowWithColor(context, offset, blurRadius, opaqueShadowColor);
CGContextSetBlendMode(context, kCGBlendModeSourceOut);
CGContextSetFillColorWithColor(context, opaqueShadowColor);
CGContextAddPath(context, fillPath);
CGContextFillPath(context);
} CGContextEndTransparencyLayer(context);
CGColorRelease(opaqueShadowColor);
} CGContextRestoreGState(context);

// outer shadow
CGContextSaveGState(context); {
CGContextAddPath(context, boxPath);
CGContextAddPath(context, fillPath);
[[UIColor colorWithWhite:0.7 alpha:1.000] setFill];
CGContextFillPath(context);
} CGContextEndTransparencyLayer(context);
} CGContextRestoreGState(context);

// stroke
CGContextSaveGState(context); {
CGContextAddPath(context, strokePath);
[[UIColor colorWithWhite:0.6 alpha:1] setStroke];
CGContextSetLineWidth(context, MPITextCGFloatFromPixel(1));
CGContextStrokePath(context);
} CGContextRestoreGState(context);

CFRelease(boxPath);
CFRelease(fillPath);
CFRelease(strokePath);

image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGContextEOClip(context);
CGColorRef shadowColor = [UIColor colorWithWhite:0 alpha:0.32].CGColor;
CGContextSetShadowWithColor(context, CGSizeMake(0, 1.5), 3, shadowColor);
CGContextBeginTransparencyLayer(context, NULL); {
CGContextAddPath(context, fillPath);
[[UIColor colorWithWhite:0.7 alpha:1.000] setFill];
CGContextFillPath(context);
} CGContextEndTransparencyLayer(context);
} CGContextRestoreGState(context);

// stroke
CGContextSaveGState(context); {
CGContextAddPath(context, strokePath);
[[UIColor colorWithWhite:0.6 alpha:1] setStroke];
CGContextSetLineWidth(context, MPITextCGFloatFromPixel(1));
CGContextStrokePath(context);
} CGContextRestoreGState(context);

CFRelease(boxPath);
CFRelease(fillPath);
CFRelease(strokePath);
}];

});
return image;
Expand Down

0 comments on commit 6829d96

Please sign in to comment.