美文网首页
提示框小助手:AlertViewHelper

提示框小助手:AlertViewHelper

作者: SMFly | 来源:发表于2016-07-17 16:59 被阅读101次

    刚开始一直使用的是UIAlertController,发现多重alert会因为多次dissmiss回到不可预见的界面,所以就谨慎使用AlertVC了,

    调起本地相册相机

    /*使用ALertController

    * @brief调起本地相机,相册

    *底部出现,数组中传递的是字符串数组

    * @param<#param#>

    */

    + (UIAlertController*)setAlertViewForAddImageWithAlertStyle:(UIAlertControllerStyle)alertStyle array:(NSArray *)titleArray local:(StatusBlock)local camera:(StatusBlock)camera {

    UIAlertController* userIconAlert = [UIAlertControlleralertControllerWithTitle:titleArray[0]message:@""preferredStyle:alertStyle];

    if(titleArray[1]) {

    UIAlertAction* chooseFromPhotoAlbum = [UIAlertActionactionWithTitle:titleArray[1]style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

    local();

    }];

    [userIconAlertaddAction:chooseFromPhotoAlbum];

    }

    if(titleArray[2]) {

    UIAlertAction* chooseFromCamera = [UIAlertActionactionWithTitle:titleArray[2]style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

    camera();

    }];

    [userIconAlertaddAction:chooseFromCamera];

    }

    UIAlertAction* canelAction = [UIAlertActionactionWithTitle:[titleArraylastObject]style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {

    //取消

    }];

    [userIconAlertaddAction:canelAction];

    returnuserIconAlert;

    }

    带有按钮的Alert

    /*

    * @brief  title为按钮字符串,message为提示信息

    *

    * @param<#param#>

    */

    + (void)setAlertWithViewController:(UIViewController*)viewController Title:(NSString*)title message:(NSString*)message cancelTitle:(NSString*)cancelTitle sureTitle:(NSString*)sureTitle cancle:(StatusBlock)cancle sure:(StatusBlock)sure {

    __weaktypeof(viewController) weakSelf = viewController;

    UIAlertController*alertVC = [UIAlertControlleralertControllerWithTitle:titlemessage:messagepreferredStyle:UIAlertControllerStyleAlert];

    if(cancelTitle) {

    UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:cancelTitlestyle:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {

    cancle();

    }];

    [alertVCaddAction:cancelAction];

    }

    if(sureTitle) {

    UIAlertAction*sureAction = [UIAlertActionactionWithTitle:sureTitlestyle:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

    sure();

    }];

    [alertVCaddAction:sureAction];

    }

    [weakSelfpresentViewController:alertVCanimated:YEScompletion:nil];

    }

    常规提示框位置固定在屏幕下部

    OC中并每次调用全部view,label都是临时创建的,方法如下

    + (UIView*)showViewAtBottomWithViewController:(UIViewController*)viewController string:(NSString*)string{

    UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(ScreenWidth/2.0-100,ScreenHeight-90,200,40)];

    view.layer.masksToBounds=YES;

    view.layer.cornerRadius=5;

    view.backgroundColor= [[UIColorblackColor]colorWithAlphaComponent:0.4];

    UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(5,5,190,30)];

    label.text= string;

    label.textColor= [UIColorwhiteColor];

    label.textAlignment=NSTextAlignmentCenter;

    label.font= [UIFontsystemFontOfSize:14];

    CGFloatwidth = [UILabelgetWidthWithTitle:stringfont:[UIFontsystemFontOfSize:14]];

    if(width >ScreenWidth-80) {

    view.frame=CGRectMake(40,ScreenHeight-90,ScreenWidth-80,40);

    label.frame=CGRectMake(5,5,ScreenWidth-80-10,30);

    }

    else

    {

    view.frame=CGRectMake((ScreenWidth- width)/2,ScreenHeight-90, width +10,40);

    label.frame=CGRectMake(5,5, width,30);

    }

    [viewaddSubview:label];

    [viewController.viewaddSubview:view];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

    [viewremoveFromSuperview];

    });

    returnview;

    }

    其中方法+ (CGFloat)getWidthWithTitle:(NSString*)title font:(UIFont*)font 是给了label类目,自适应提示信息高度不完美,

    + (CGFloat)getWidthWithTitle:(NSString*)title font:(UIFont*)font {

    UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(0,0,1000,0)];

    label.text= title;

    label.font= font;

    [labelsizeToFit];

    returnlabel.frame.size.width;

    }

    OC中这个方法没有完善,1.每次调用新建空间,2.提示信息没那么长所有没有给定限制,

    swift中完善了这个方法

    import UIKit

    class AlertViewHelper:NSObject{

    static let shareInstance =AlertViewHelper()

    private override init() {

    }//单例

    private var view =UIView()

    private var label =UILabel()

    private var size:CGSize?

    //MARK: MARK标签----底部提示框

    /*

    * @brief<#Function#>

    *

    * @param<#param#>

    */

    func showViewAtBottom(target:UIView,string:String) ->UIView{

    if target.subviews.contains(view) {

    view.removeFromSuperview()

    }

    view.frame=CGRectMake(SCREENWIDTH/2.0-100,SCREENHEIGHT-90,200,40)

    view.layer.masksToBounds=true

    view.layer.cornerRadius=5

    view.backgroundColor=UIColor.blackColor().colorWithAlphaComponent(0.4)

    label.frame=CGRectMake(5,5,190,30)

    label.text= string

    label.textColor= .whiteColor()

    label.textAlignment= .Center

    label.font=UIFont.systemFontOfSize(14)

    label.numberOfLines=0

    size=RequestHelper.getLabelSize(CGSizeMake(CGFloat(MAXFLOAT),CGFloat(MAXFLOAT)), string: string, font:UIFont.systemFontOfSize(14))

    ifInt(size!.width) >Int(SCREENWIDTH) -60{

    size=getLabelSize(CGSizeMake((CGFloat(SCREENWIDTH) -60),CGFloat(MAXFLOAT)), string: string, font:UIFont.systemFontOfSize(14))

    }

    label.frame=CGRectMake(10,5,size!.width,size!.height)

    view.frame=CGRectMake((SCREENWIDTH-size!.width)/2.0-20,SCREENHEIGHT-50-size!.height-5,size!.width+20,size!.height+10)

    view.addSubview(label)

    target.addSubview(view)

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,Int64(1.0*Double(NSEC_PER_SEC))),dispatch_get_main_queue()) {

    iftarget.subviews.contains(self.view) {

    self.view.removeFromSuperview()

    }

    }

    returnview

    }

    //MARK: MARK标签----获取文字的长度和高度

    /*

    * @brief需要显示一行,则size宽高均为最大,定宽时高度为最大

    *

    * @param<#param#>

    */

    func getLabelSize(size:CGSize,string:String,font:UIFont) ->CGSize{

    let str = string as NSString

    return(str.boundingRectWithSize(size, options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName:font], context:nil).size)

    }

    }

    中心状态显示

    没怎么用

    /*

    * @brief状态显示

    *

    * @param<#param#>

    */

    + (UIActivityIndicatorView*)setAlertViewAndFlowersWithViewController:(UIViewController*)viewController {

    UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(ScreenWidth/2.0-40,ScreenHeight/2.0-40,80,80)];

    view.backgroundColor= [[UIColorblackColor]colorWithAlphaComponent:0.5];

    view.layer.masksToBounds=YES;

    view.layer.cornerRadius=5;

    [viewController.viewaddSubview:view];

    UIActivityIndicatorView*flowerView = [[UIActivityIndicatorViewalloc]initWithFrame:CGRectMake(15,15,50,50)];

    flowerView.activityIndicatorViewStyle=UIActivityIndicatorViewStyleWhite;

    flowerView.hidesWhenStopped=YES;

    [flowerViewstartAnimating];

    [viewaddSubview:flowerView];

    returnflowerView;

    }

    含有lablel和TextField的多行提示框

    + (void)alertControllerWithTitle:(NSString*)title message:(NSString*)message viewController:(UIViewController*)viewController preferredStyle:(UIAlertControllerStyle)preferredStyle textFieldPlaceHolders:(NSArray *) placeholderssureHandel:(void(^)(NSArray* alterTextFs)) sureHandel cancelHandel:(void(^)()) cancelHander{

    __weaktypeof(viewController) weakSelf = viewController;

    UIAlertController*alertC = [UIAlertControlleralertControllerWithTitle:titlemessage:messagepreferredStyle:preferredStyle];

    UIAlertAction*shureAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

    if(sureHandel) {

    NSMutableArray*temArr = [NSMutableArrayarray];

    [alertC.textFieldsenumerateObjectsUsingBlock:^(UITextField*_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {

    [temArraddObject:obj.text];

    }];

    sureHandel([temArrcopy]);

    }

    }];

    UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {

    if(cancelHander) {

    cancelHander();

    }

    }];

    [alertCaddAction:shureAction];

    [alertCaddAction:cancelAction];

    //plcaceholder

    [placeholdersenumerateObjectsUsingBlock:^(NSString*_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {

    [alertCaddTextFieldWithConfigurationHandler:^(UITextField*_NonnulltextField) {

    textField.placeholder= placeholders[idx];

    }];

    }];

    [weakSelfpresentViewController:alertCanimated:YEScompletion:nil];

    }

    相关文章

      网友评论

          本文标题:提示框小助手:AlertViewHelper

          本文链接:https://www.haomeiwen.com/subject/eytpjttx.html