PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV Mobile 24 → Toast Message iOS - scales according to the size of text = 100% ok
Toast Message iOS - scales according to the size of text = 100% ok
Débuté par Adriano Boller, 03 nov. 2014 16:44 - 2 réponses
Posté le 03 novembre 2014 - 16:44
//Toast Message iOS - scales according to the size of text = 100% ok
//-------------------------------------------------------------------------------
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <MediaPlayer/MediaPlayer.h>
void IOS(){}
//////////////////////////////////////////////////
// Classes
//////////////////////////////////////////////////

// Propriétés des toasts
#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 20
#define TOP_BOTTOM_BORDER 20

//#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
//#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#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 SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
#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;
// Crée un toast
+ (WDToast *)toastWithMessage:(NSString *)msg delay:(int)nDelai;
// Affiche le toast
- (void)showOnView:(UIView *)mainView;
@end

// Description de l'interface WDSaisie
@interface WDSaisie : UIViewController <UIAlertViewDelegate>
{
// Conserve la valeur saisie
NSString *ValeurSaisie;
// Conserve le bouton cliqué
NSInteger BoutonClicIdx;
// Bouton cliqué
BOOL m_bClic;
}
@property (nonatomic, retain) NSString *ValeurSaisie;
@property (nonatomic) NSInteger BoutonClicIdx;
// Affiche la boîte de saisie
-(void)Affiche:(NSString*)Question avecTexteAide:(NSString*)TexteAide avecFormatSaisie:(UIKeyboardType)FormatSaisie avecValeurDefaut:(NSString*)ValeurDefaut;
@end

// Description de l'interface WDSaisieMotDePasse
@interface WDSaisieMotDePasse : WDSaisie
{
// Conserve la valeur saisie
NSString *MotDePasse;
}
@property (nonatomic, retain) NSString *MotDePasse;
// Affiche la boîte de saisie
-(void)Affiche:(NSString*)Question avecTexteAide:(NSString*)TexteAide avecValeurDefaut:(NSString*)ValeurDefaut;
// Récupère la valeur saisie
@end

//////////////////////////////////////////////////
// Implémentations
//////////////////////////////////////////////////

// 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];

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


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)) {
// version >= 7.0

CGSize size2 = [msg boundingRectWithSize:CGSizeMake(wScreen - LEFT_RIGTH_BORDER_EXTERN - LEFT_RIGTH_BORDER_INTERN, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName : font
}
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.font = [UIFont fontWithName:@"Courier" size: 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

// Implémentation de l'interface WDSaisie
@implementation WDSaisie
@synthesize ValeurSaisie;
@synthesize BoutonClicIdx;
// Affiche(Question,TexteAide)
-(void)Affiche:(NSString*)Question avecTexteAide:(NSString*)TexteAide avecFormatSaisie:(UIKeyboardType)FormatSaisie avecValeurDefaut:(NSString*)ValeurDefaut
{
// Crée une boîte de dialogue avec les boutons OK et Annuler
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:Question delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Annuler",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
// Récupère le champ de saisie
UITextField *alertTextField = [alert textFieldAtIndex:0];
// Définit le format de saisie
alertTextField.keyboardType = FormatSaisie;
// Définit le texte d'aide
alertTextField.placeholder = TexteAide;
// Définit la valeur par défaut
alertTextField.text = ValeurDefaut;
// Pas de clic
m_bClic = NO;
// Affiche la boîte de dialogue
[alert show];
// Boucle d'attente
do {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
} while (!m_bClic);
// Libère l'objet
[alert release];
}
// Délégué appelé lors de la validation de la boîte de saisie
-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Conserve la valeur saisie
ValeurSaisie = [[NSString alloc] initWithString:[[alertView textFieldAtIndex:0] text]];
// Conserve le bouton cliqué
BoutonClicIdx = buttonIndex;
// Clic
m_bClic = YES;
}
@end

// Implémentation de l'interface WDSaisieMotDePasse
@implementation WDSaisieMotDePasse
@synthesize MotDePasse;
// Affiche(Question,TexteAide)
-(void)Affiche:(NSString*)Question avecTexteAide:(NSString*)TexteAide avecValeurDefaut:(NSString*)ValeurDefaut
{
// Crée une boîte de dialogue avec les boutons OK et Annuler
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:Question delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Annuler",nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
// Récupère le champ de saisie
UITextField *alertTextField = [alert textFieldAtIndex:0];
// Définit le texte d'aide
alertTextField.placeholder = TexteAide;
// Définit la valeur par défaut
alertTextField.text = ValeurDefaut;
// Pas de clic
m_bClic = NO;
// Affiche la boîte de dialogue
[alert show];
// Boucle d'attente
do {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
} while (!m_bClic);
// Libère l'objet
[alert release];
}
// Délégué appelé lors de la validation de la boîte de saisie
-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Conserve la valeur saisie et le mot de passe
ValeurSaisie = [[NSString alloc] initWithString:[[alertView textFieldAtIndex:0] text]];
MotDePasse = [[NSString alloc] initWithString:[[alertView textFieldAtIndex:1] text]];
// Conserve le bouton cliqué
BoutonClicIdx = buttonIndex;
// Clic
m_bClic = YES;
}
@end
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
void IOS_SetAutolockDisabled(BOOL bStatus)
{
// Changes the status of the autolock
[[UIApplication sharedApplication] setIdleTimerDisabled:bStatus];
}
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
void IOS_ToastAffiche(NSString *sMessage, void *pclView, int nDelai)
{
WDToast *pclToast = [WDToast toastWithMessage:sMessage delay:nDelai];
[pclToast showOnView:(UIView *)pclView];
}
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
//Toast in iOS
Tempo is int = 0

IF Mensagem <> "" THEN

IF CurtoLongo = "C"
Tempo = 2
ELSE
Tempo = 5
END

IOS_ToastAffiche(Mensagem, Handle(), Tempo)// esta travando a aplicacao no ios

END

----
CODE OK
Membre enregistré
3 003 messages
Popularité : +89 (91 votes)
Posté le 17 novembre 2014 - 13:12
#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 003 messages
Popularité : +89 (91 votes)
Posté le 17 novembre 2014 - 13:12
void IOS_ToastAffiche(NSString *sMessage, void *pclView, int nDelai)
{
WDToast *pclToast = [WDToast toastWithMessage:sMessage delay:nDelai];
[pclToast showOnView:(UIView *)pclView];
}