PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV Mobile 2024 → Toast in iOS
Toast in iOS
Débuté par adrianoboller, 17 nov. 2014 13:14 - 2 réponses
Membre enregistré
3 657 messages
Popularité : +175 (223 votes)
Posté le 17 novembre 2014 - 13:14
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <MediaPlayer/MediaPlayer.h>

void IOS(){}

//////////////////////////////////////////////////
// Classes
//////////////////////////////////////////////////
#define FONT_SIZE 14
#define LEFT_RIGHT_PADDING 14
#define TOP_BOTTOM_PADDING 10
#define BOTTOM_MARGIN 200

#define BACKGROUND_COLOR darkGrayColor
#define FADE_IN_DURATION 0.4
#define FADE_OUT_DURATION 0.3
#define DELAY 2
#define MAX_CHAR_LINHA 30
#define MAX_LENGTH_LABEL 300
#define LEFT_RIGTH_BORDER_INTERN 10
#define LEFT_RIGTH_BORDER_EXTERN 20
#define TOP_BOTTOM_BORDER_INTERN 10

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define iOS7_0 @"7.0"


// Toast
@interface WDToast : NSObject
// Message
@Property (nonatomic, strong) NSString *Message;
// Durée
@Property (nonatomic) int Delai;
// Champ libellé
@Property (nonatomic, strong) UILabel *label;
+ (WDToast *)toastWithMessage:(NSString *)msg delay:(int)nDelai;
// Affiche le toast
- (void)showOnView:(UIView *)mainView;
@END

// Implémentation de l'interface WDToast
@implementation WDToast
{
CGRect rect;
}

+ (WDToast *)toastWithMessage:(NSString *)msg delay:(int)nDelai;
{
WDToast *t = [[WDToast alloc] init];
t.message=msg;
t.Delai = nDelai;

//Resolucao
float xLabel;
float yLabel;
float wScreen;
float hScreen;
float hLablel;
float wLabel;
int countLines = 0;



CGSize sz = [UIScreen mainScreen].bounds.size;
wScreen = sz.width;
hScreen = sz.height;

UIFont *font = [UIFont systemFontOfSize:FONT_SIZE];

// version < 7.0
IF (SYSTEM_VERSION_LESS_THAN(iOS7_0)) {

//Adriano suprime warning//
#pragma clang diagnostic Push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
//Adriano suprime warning//

CGSize withinSize = CGSizeMake(wScreen - LEFT_RIGTH_BORDER_EXTERN - LEFT_RIGTH_BORDER_INTERN, FLT_MAX);
CGSize size = [msg sizeWithFont:font constrainedToSize:withinSize lineBreakMode:NSLineBreakByWordWrapping];

wLabel = size.width + LEFT_RIGTH_BORDER_INTERN;
hLablel = size.height + TOP_BOTTOM_BORDER_INTERN;

} ELSE IF (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(iOS7_0)) {

CGSize size2 = [msg boundingRectWithSize:CGSizeMake(wScreen - LEFT_RIGTH_BORDER_EXTERN - LEFT_RIGTH_BORDER_INTERN, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName : font

}
// version >= 7.0

context:nil].size;


wLabel = size2.width + LEFT_RIGTH_BORDER_INTERN;
hLablel = size2.height + TOP_BOTTOM_BORDER_INTERN;
} ELSE

{
countLines = (([msg Length])/ MAX_CHAR_LINHA )+ 1;
IF (countLines > 1 )
{

wLabel = MAX_LENGTH_LABEL;

}ELSE
{

wLabel = ([msg Length] * 8) + FONT_SIZE;

}
};

countLines = 30;

//Meio do iphone 3 e 4 = W: 160 e H: 240
xLabel = (wScreen / 2) - (wLabel / 2);
yLabel = (hScreen / 2);

//Desenha retangulo do Toast
CGRect rect = CGRectMake(xLabel,yLabel,wLabel,hLablel);
t.label = [[UILabel alloc] initWithFrame:rect];
t.label.text = msg;
t.label.textColor = [UIColor whiteColor];
t.label.font = [UIFont systemFontOfSize:FONT_SIZE];
t.label.textAlignment = NSTextAlignmentCenter;
t.label.backgroundColor = [UIColor BACKGROUND_COLOR];
t.label.numberOfLines = countLines;

RETURN t;
}
- (void)showOnView:(UIView *)view
{
[self.label setAlpha:0];
[view addSubview:self.label];

[UIView animateWithDuration:FADE_IN_DURATION
animations:^{
[self.label setAlpha:1];
}
completion:^(BOOL finished) {
[UIView animateWithDuration:FADE_OUT_DURATION
delay:self.Delai
options:0
animations:^{ [self.label setAlpha:0]; }
completion:^(BOOL finished) {}
];
}];
}
@END
Membre enregistré
3 657 messages
Popularité : +175 (223 votes)
Posté le 17 novembre 2014 - 13:14
void IOS_ToastAffiche(NSString *sMessage, void *pclView, int nDelai)
{
WDToast *pclToast = [WDToast toastWithMessage:sMessage delay:nDelai];
[pclToast showOnView:(UIView *)pclView];
}
Membre enregistré
96 messages
Popularité : +1 (1 vote)
Posté le 11 mai 2015 - 23:54
bonjour, ne fonctionne pas chez moi une astuce ?
j'ai remplacé
void IOS(){}
par
void IOS_ToastAffiche(NSString *sMessage, void *pclView, int nDelai)
{
WDToast *pclToast = [WDToast toastWithMessage:sMessage delay:nDelai];
[pclToast showOnView:(UIView *)pclView];
}

mais pas bon :(