天天看點

iOS開發之自己封裝的提示框(警告框)樣式BHAlertView

最近需要使用到提示框(警告框)進行資訊的展示和提醒,是以進行了一個類的封裝,想用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];

效果圖:

iOS開發之自己封裝的提示框(警告框)樣式BHAlertView
iOS開發之自己封裝的提示框(警告框)樣式BHAlertView
iOS開發之自己封裝的提示框(警告框)樣式BHAlertView
iOS開發之自己封裝的提示框(警告框)樣式BHAlertView
iOS開發之自己封裝的提示框(警告框)樣式BHAlertView