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

update pod spec & remove non ARC codes #46

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion iToast.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ typedef enum {

- (void) show;
- (void) show:(iToastType) type;
- (iToast *) setPadding:(CGFloat ) padding;
- (iToast *) setEdgeInsets:(UIEdgeInsets ) insets;
- (iToast *) setLineSpacing:(CGFloat ) lineSpace;
- (iToast *) setDuration:(NSInteger ) duration;
- (iToast *) setGravity:(iToastGravity) gravity
offsetLeft:(NSInteger) left
Expand Down Expand Up @@ -124,10 +127,12 @@ typedef enum {
@property(assign) NSInteger offsetTop;
@property(readonly) NSDictionary *images;
@property(assign) iToastImageLocation imageLocation;
@property(assign) UIEdgeInsets insets;
@property(assign) CGFloat lineSpace;


- (void) setImage:(UIImage *)img forType:(iToastType) type;
- (void) setImage:(UIImage *)img withLocation:(iToastImageLocation)location forType:(iToastType)type;
+ (iToastSettings *) getSharedSettings;

@end
@end
104 changes: 81 additions & 23 deletions iToast.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,41 @@ - (void) show:(iToastType) type {
UIImage *image = [theSettings.images valueForKey:[NSString stringWithFormat:@"%i", type]];

UIFont *font = [UIFont systemFontOfSize:theSettings.fontSize];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = theSettings.lineSpace;
if (theSettings.lineSpace == 0.f) {
paragraphStyle.lineSpacing = 1.f;
}

NSAttributedString *attributedText =[[NSAttributedString alloc] initWithString:text attributes:@{ NSFontAttributeName: font}];
CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(280, 60)
CGRect screenBounds = [UIScreen mainScreen].bounds;
CGFloat toastMaxWidth = screenBounds.size.width - 40.f;
NSAttributedString *attributedText =[[NSAttributedString alloc] initWithString:text attributes:@{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle
}];
CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(toastMaxWidth, 60)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize textSize = rect.size;

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width + kComponentPadding, textSize.height + kComponentPadding)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.font = font;
label.text = text;
if (rect.size.height == font.lineHeight + paragraphStyle.lineSpacing) {
textSize.height -= paragraphStyle.lineSpacing;
}

UIEdgeInsets insets = theSettings.insets;
if (UIEdgeInsetsEqualToEdgeInsets(insets, UIEdgeInsetsZero)) {
insets = UIEdgeInsetsMake(kComponentPadding, kComponentPadding, kComponentPadding, kComponentPadding);
}
CGFloat paddingForWidth = insets.left + insets.right;
CGFloat paddingForHeight = insets.top + insets.bottom;

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(insets.left, insets.top, textSize.width, textSize.height)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.font = font;
if (rect.size.height == font.lineHeight + paragraphStyle.lineSpacing) {
label.text = text;
} else {
label.attributedText = attributedText;
}
label.numberOfLines = 0;
if (theSettings.useShadow) {
label.shadowColor = [UIColor darkGrayColor];
Expand All @@ -96,36 +119,34 @@ - (void) show:(iToastType) type {
switch ([theSettings imageLocation]) {
case iToastImageLocationLeft:
[label setTextAlignment:NSTextAlignmentLeft];
label.center = CGPointMake(image.size.width + kComponentPadding * 2
+ (v.frame.size.width - image.size.width - kComponentPadding * 2) / 2,
label.center = CGPointMake(image.size.width + paddingForWidth
+ (v.frame.size.width - image.size.width - paddingForWidth) / 2,
v.frame.size.height / 2);
break;
case iToastImageLocationTop:
[label setTextAlignment:NSTextAlignmentCenter];
label.center = CGPointMake(v.frame.size.width / 2,
(image.size.height + kComponentPadding * 2
+ (v.frame.size.height - image.size.height - kComponentPadding * 2) / 2));
(image.size.height + paddingForHeight
+ (v.frame.size.height - image.size.height - paddingForHeight) / 2));
break;
default:
break;
}

} else {
v.frame = CGRectMake(0, 0, textSize.width + kComponentPadding * 2, textSize.height + kComponentPadding * 2);
v.frame = CGRectMake(0, 0, textSize.width + paddingForWidth, textSize.height + paddingForHeight);
label.center = CGPointMake(v.frame.size.width / 2, v.frame.size.height / 2);
}
CGRect lbfrm = label.frame;
lbfrm.origin.x = ceil(lbfrm.origin.x);
lbfrm.origin.y = ceil(lbfrm.origin.y);
label.frame = lbfrm;
[v addSubview:label];
[label release];

if (image) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = [self _frameForImage:type inToastFrame:v.frame];
[v addSubview:imageView];
[imageView release];
}

v.backgroundColor = [UIColor colorWithRed:theSettings.bgRed green:theSettings.bgGreen blue:theSettings.bgBlue alpha:theSettings.bgAlpha];
Expand Down Expand Up @@ -242,23 +263,37 @@ - (void) show:(iToastType) type {
v.alpha = 1;
[UIView commitAnimations];

view = [v retain];
view = v;

[v addTarget:self action:@selector(hideToast:) forControlEvents:UIControlEventTouchDown];
}

- (CGRect)_toastFrameForImageSize:(CGSize)imageSize withLocation:(iToastImageLocation)location andTextSize:(CGSize)textSize {
CGRect theRect = CGRectZero;

iToastSettings *theSettings = _settings;

if (!theSettings) {
theSettings = [iToastSettings getSharedSettings];
}

UIEdgeInsets insets = theSettings.insets;
if (UIEdgeInsetsEqualToEdgeInsets(insets, UIEdgeInsetsZero)) {
insets = UIEdgeInsetsMake(kComponentPadding, kComponentPadding, kComponentPadding, kComponentPadding);
}
CGFloat paddingForWidth = insets.left + insets.right;
CGFloat paddingForHeight = insets.top + insets.bottom;

switch (location) {
case iToastImageLocationLeft:
theRect = CGRectMake(0, 0,
imageSize.width + textSize.width + kComponentPadding * 3,
MAX(textSize.height, imageSize.height) + kComponentPadding * 2);
imageSize.width + textSize.width + paddingForWidth,
MAX(textSize.height, imageSize.height) + paddingForHeight);
break;
case iToastImageLocationTop:
theRect = CGRectMake(0, 0,
MAX(textSize.width, imageSize.width) + kComponentPadding * 2,
imageSize.height + textSize.height + kComponentPadding * 3);
MAX(textSize.width, imageSize.width) + paddingForWidth,
imageSize.height + textSize.height + paddingForHeight);

default:
break;
Expand All @@ -274,12 +309,17 @@ - (CGRect)_frameForImage:(iToastType)type inToastFrame:(CGRect)toastFrame {

CGRect imageFrame = CGRectZero;

UIEdgeInsets insets = theSettings.insets;
if (UIEdgeInsetsEqualToEdgeInsets(insets, UIEdgeInsetsZero)) {
insets = UIEdgeInsetsMake(kComponentPadding, kComponentPadding, kComponentPadding, kComponentPadding);
}

switch ([theSettings imageLocation]) {
case iToastImageLocationLeft:
imageFrame = CGRectMake(kComponentPadding, (toastFrame.size.height - image.size.height) / 2, image.size.width, image.size.height);
imageFrame = CGRectMake(insets.left, (toastFrame.size.height - image.size.height) / 2, image.size.width, image.size.height);
break;
case iToastImageLocationTop:
imageFrame = CGRectMake((toastFrame.size.width - image.size.width) / 2, kComponentPadding, image.size.width, image.size.height);
imageFrame = CGRectMake((toastFrame.size.width - image.size.width) / 2, insets.top, image.size.width, image.size.height);
break;

default:
Expand Down Expand Up @@ -307,11 +347,25 @@ - (void) removeToast:(NSTimer*)theTimer{


+ (iToast *) makeText:(NSString *) _text{
iToast *toast = [[[iToast alloc] initWithText:_text] autorelease];
iToast *toast = [[iToast alloc] initWithText:_text];

return toast;
}

- (iToast *) setPadding:(CGFloat ) padding {
[self theSettings].insets = UIEdgeInsetsMake(padding, padding, padding, padding);
return self;
}

- (iToast *) setEdgeInsets:(UIEdgeInsets ) insets {
[self theSettings].insets = insets;
return self;
}

- (iToast *) setLineSpacing:(CGFloat ) lineSpace {
[self theSettings].lineSpace = lineSpace;
return self;
}

- (iToast *) setDuration:(NSInteger ) duration{
[self theSettings].duration = duration;
Expand Down Expand Up @@ -438,6 +492,8 @@ + (iToastSettings *) getSharedSettings{
sharedSettings.bgAlpha = 0.7;
sharedSettings.offsetLeft = 0;
sharedSettings.offsetTop = 0;
sharedSettings.insets = UIEdgeInsetsZero;
sharedSettings.lineSpace = 1.f;
}

return sharedSettings;
Expand All @@ -458,6 +514,8 @@ - (id) copyWithZone:(NSZone *)zone{
copy.bgAlpha = self.bgAlpha;
copy.offsetLeft = self.offsetLeft;
copy.offsetTop = self.offsetTop;
copy.insets = self.insets;
copy.lineSpace = self.lineSpace;

NSArray *keys = [self.images allKeys];

Expand Down
4 changes: 2 additions & 2 deletions iToast.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

Pod::Spec.new do |s|
s.name = 'iToast'
s.version = "0.0.1"
s.version = "1.1.0"
s.summary = 'An Objective-C iOS way to display non intrusive messages to the user like in Android.'
s.homepage = 'https://github.com/ecstasy2/toast-notifications-ios'
s.license = { :type => 'MIT', :file => 'LICENSE.txt' }
s.author = 'DIALLO Mamadou Bobo'
s.source = { :git => 'https://github.com/ecstasy2/toast-notifications-ios.git' ,:commit => '68ea9a1'}
s.source = { :git => 'https://github.com/ecstasy2/toast-notifications-ios.git' ,:tag => '1.1.0'}
s.source_files = '*.{h,m}'
s.framework = 'QuartzCore'
s.requires_arc = true
Expand Down