先看代码:
#import <UIKit/UIKit.h>
@interface AlertView : UIView
- (void)showWithText:(NSString *)text;
- (void)showWithText:(NSString *)text Center:(CGPoint)center;
- (void)showTextOnWindows:(NSString *)text;
@end
#import "AlertView.h"
#import <QuartzCore/QuartzCore.h>
@interface AlertView()
@property (strong, nonatomic) UITextView *textView;
@property (strong, nonatomic) UIView *markView;
@end
@implementation AlertView
- (id)init
{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 120, 100);
self.backgroundColor = [UIColor clearColor];
self.markView = [[UIView alloc] initWithFrame:self.bounds];
self.markView.backgroundColor = [UIColor blackColor];
self.markView.layer.cornerRadius = 8;
self.markView.alpha = 0.7f;
[self addSubview:self.markView];
self.textView = [[UITextView alloc] initWithFrame:self.bounds];
self.textView.backgroundColor = [UIColor clearColor];
self.textView.font = [UIFont fontWithName:@"Heiti SC" size:16];
self.textView.textColor = [UIColor whiteColor];
self.textView.userInteractionEnabled = NO;
self.textView.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.textView];
self.alpha = 0;
}
return self;
}
- (void)showWithText:(NSString *)text
{
self.textView.text = text;
CGFloat width, height;
int col, mod, lengthCount;
width = 240;
lengthCount = 12;
}else {
width = 120;
lengthCount = 6;
}
col = text.length / lengthCount ;
mod = text.length % lengthCount;
if (mod != 0) {
col = col + 1;
}
col = col + 1;
height = 17 * col;
self.frame = CGRectMake(0, 0, width, height + 5);
self.markView.frame = self.bounds;
self.textView.frame = CGRectMake(0, 5, width, height);
self.center = self.superview.center;
self.alpha = 1.0f;
[self performSelector:@selector(closeMe) withObject:nil afterDelay:1];
}
- (void)showWithText:(NSString *)text Center:(CGPoint)center
{
self.textView.text = text;
CGFloat width, height;
int col, mod, lengthCount;
if ([[UIScreen mainScreen] bounds].size.width >= 768) {
width = 240;
lengthCount = 12;
}else {
width = 120;
lengthCount = 6;
}
col = text.length / lengthCount ;
mod = text.length % lengthCount;
if (mod != 0) {
col = col + 1;
}
col = col + 1;
height = 17 * col;
self.frame = CGRectMake(0, 0, width, height + 5);
self.markView.frame = self.bounds;
self.textView.frame = CGRectMake(0, 5, width, height);
self.center = center;
self.alpha = 1.0f;
[self performSelector:@selector(closeMe) withObject:nil afterDelay:1];
}
- (void)closeMe
{
self.alpha = 0;
}
- (void)showTextOnWindows:(NSString *)text
{
self.textView.text = text;
CGFloat width, height;
int col, mod, lengthCount;
if ([[UIScreen mainScreen] bounds].size.width >= 768) {
width = 240;
lengthCount = 12;
}else {
width = 120;
lengthCount = 6;
}
col = text.length / lengthCount ;
mod = text.length % lengthCount;
if (mod != 0) {
col = col + 1;
}
col = col + 1;
height = 17 * col;
self.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2 - width/2, [[UIScreen mainScreen] bounds].size.height/2 - height/2, width, height + 5);
self.markView.frame = self.bounds;
self.textView.frame = CGRectMake(0, 5, width, height);
self.alpha = 1.0f;
[[[UIApplication sharedApplication] delegate].window addSubview:self];
[self performSelector:@selector(closeMeOnWindows) withObject:nil afterDelay:2];
}
- (void)closeMeOnWindows
{
self.alpha = 0;
[self removeFromSuperview];
}
@end
使用方法举例:(跨界面的弹窗)
AlertView *alertView = [[AlertView alloc] init];
[alertView showTextOnWindows:@"请输入姓名"];