Skip to content
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

Use UIGraphicsImageRenderer instead of UIGraphicsBeginImageContextWithOptions #54

Merged
merged 3 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading