最近需要使用到提示框(警告框)进行信息的展示和提醒,所以进行了一个类的封装,想用Swift调用此OC文件,但是发现有些困难,所以暂时先把OC代码进行展示,随后再好好研究一下在Swift中的使用。
对于封装文件,首先要设计界面,其次是数据之间的传递过程
初始化样式方法:
- (instancetype)initWithTitle:(NSString *)title icon:(UIImage *)icon message:(NSString *)message delegate:(id<BHAlertViewDelegate>)delegate buttonTitles:(NSString *)buttonTitles, ... {
if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
_icon = icon;
_title = title;
_message = message;
_delegate = delegate;
_buttonArray = [NSMutableArray array];
_buttonTitleArray = [NSMutableArray array];
//在C中,当我们无法列出传递函数的所有实参的类型和数目时,可以用省略号指定参数表,下面就是为了实现方法中"..."的效果
va_list args;
va_start(args, buttonTitles);
if (buttonTitles)
{
[_buttonTitleArray addObject:buttonTitles];
while (1)
{
NSString * otherButtonTitle = va_arg(args, NSString *);
if(otherButtonTitle == nil) {
break;
} else {
[_buttonTitleArray addObject:otherButtonTitle];
}
}
}
va_end(args);
self.backgroundColor = [UIColor clearColor];
_backgroundView = [[UIView alloc] initWithFrame:self.frame];
_backgroundView.backgroundColor = [UIColor blackColor];
[self addSubview:_backgroundView];
[self initContentView];
}
return self;
}
获取标题、信息的大小
//----------获取各控件Size大小
- (CGSize)getTitleSize {
UIFont *font = [UIFont systemFontOfSize:TITLE_FONT_SIZE];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize size = [_title boundingRectWithSize:CGSizeMake(contentViewWidth - (MARGIN_LEFT_SMALL + MARGIN_RIGHT_SMALL + _iconImageView.frame.size.width + SPACE_SMALL), 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
size.width = ceil(size.width);
size.height = ceil(size.height);
return size;
}
- (CGSize)getMessageSize {
UIFont *font = [UIFont systemFontOfSize:MESSAGE_FONT_SIZE];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = MESSAGE_LINE_SPACE;
NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize size = [_message boundingRectWithSize:CGSizeMake(contentViewWidth - (MARGIN_LEFT_LARGE + MARGIN_RIGHT_LARGE), 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
size.width = ceil(size.width);
size.height = ceil(size.height);
return size;
}
设置代理方法
//----------按钮点击执行代理方法
- (void)buttonWithPressed:(UIButton *)button {
if (_delegate && [_delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
NSInteger index = [_buttonTitleArray indexOfObject:button.titleLabel.text];
[_delegate alertView:self clickedButtonAtIndex:index];
}
[self hide];
}
然后添加一些设置字体大小颜色,显示,隐藏的方法
最后是对封装类的使用
BHAlertView *bhAlertV = [[BHAlertView alloc] initWithTitle:@"设置按钮字体的颜色和大小" icon:[UIImage imageNamed:@"baby_alert_icon"] message:@"设置按钮字体的颜色和大小设置按钮字体的颜色和大小" delegate:self buttonTitles:@"取消",@"修改",@"删除", nil];
//设置标题和内容的字体颜色、大小
[bhAlertV setTitleColor:[UIColor redColor] fontSize:17];
[bhAlertV setMessageColor:[UIColor cyanColor] fontSize:12];
//设置按钮字体的颜色和大小
[bhAlertV setButtonTitleColor:[UIColor orangeColor] fontSize:15 atIndex:0];
[bhAlertV setButtonTitleColor:[UIColor blueColor] fontSize:17 atIndex:1];
[bhAlertV setButtonTitleColor:[UIColor purpleColor] fontSize:19 atIndex:2];
[bhAlertV show];
效果图:
源码下载:http://download.csdn.net/detail/hbblzjy/9658209
如有问题,请留言。。。