zoukankan      html  css  js  c++  java
  • 自定义UIAlertView

     

    终于到了周末了,可以抽点时间写自己的第一篇原创文章了,这是本人第一次写文章,请大家多捧场,写得不好请见谅,当然有任何问题也可以给我留言,非常乐意和大家交流学习。

    支持原创,转载请注明出处。

    其实这个例子是从网上找到的,自己研究了一下,终于搞明白了如何自定义UIAlertView,将其修改之后为大家分享。

    CustomAlertView.h
    @interface CustomAlertView : UIAlertView
    
    @end
    @implementation CustomAlertView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    -(void)layoutSubviews{//这个是重点!!重新定义UIAlertView中的各种控件,包括UILabel,UIButton等
        for (UIView *v in self.subviews) {//遍历UIAlertView中的所有控件(UIView),再将它们重新设置
            if ([v isKindOfClass:[UILabel class]]) {//设置Label
                UILabel *label = (UILabel *)v;
                if ([label.text isEqualToString:self.title]) {
                    label.font = [UIFont boldSystemFontOfSize:40];
                    label.numberOfLines = 0;
                    label.lineBreakMode = UILineBreakModeWordWrap;
                    label.textColor = [UIColor redColor];
                    label.backgroundColor = [UIColor clearColor];
                    label.textAlignment = UITextAlignmentCenter;
                    label.shadowColor = [UIColor blackColor];
                    label.shadowOffset = CGSizeMake(0, -1);//设置阴影
                    [label sizeToFit];//文字大小自动适应
                    label.center = CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height*0.2);
                }else{
                    label.font = [UIFont boldSystemFontOfSize:20];
                    label.numberOfLines = 0;
                    label.lineBreakMode = UILineBreakModeWordWrap;
                    label.textColor = [UIColor yellowColor];
                    label.backgroundColor = [UIColor clearColor];
                    label.textAlignment = UITextAlignmentCenter;
                    label.shadowColor = [UIColor blackColor];
                    label.shadowOffset = CGSizeMake(0, -1);
                    label.center = CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height*0.3);
                }
            }
            if ([v isKindOfClass:NSClassFromString(@"UIAlertButton")]) {//设置Button
                UIButton *button = (UIButton *)v;
                UIImage *image = nil;
                if (button.tag == 1) {//给button的标签赋一个值,以便区分左右两个Button,对其进行更详细的设置。
                    image = [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", @"red"]];
                }else{
                    image = [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", @"black"]];
                }
                image = [image stretchableImageWithLeftCapWidth:(int)(image.size.width+1)>>1 topCapHeight:0];//这句话的作用是:将image图片的内容拉伸,而边角不拉伸。>>是右移运算符,感兴趣的同学可以去百度一下,这里不需要知道原理,了解作用就行了
                button.titleLabel.font = [UIFont boldSystemFontOfSize:18];
                button.titleLabel.minimumFontSize = 10;
                button.titleLabel.textAlignment = UITextAlignmentCenter;
                button.titleLabel.shadowOffset = CGSizeMake(0, -1);
                button.backgroundColor = [UIColor clearColor];
                [button setBackgroundImage:image forState:UIControlStateNormal];
                if (button.tag == 1)
                button.center = CGPointMake(self.bounds.size.width*0.25, self.bounds.size.height*0.75);
                else
                    button.center = CGPointMake(self.bounds.size.width*0.75, self.bounds.size.height*0.5);
    //            [button setTitleColor:kAlertViewButtonTextColor forState:UIControlStateNormal];
                [button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }
    }
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
        UIImageView *pic = [[UIImageView alloc]initWithFrame:CGRectMake(230, 45, 60, 60)];
        [pic setImage:[UIImage imageNamed:@"LittleWhitePig.png"]];
        [self addSubview:pic];
        [pic release];
    }
    
    - (void)show
    {
        [super show];
        self.bounds = CGRectMake(0, 100, 320, 300);//更改整个UIAlertView的框架
    }
    
    @end

    代码很简单,重点-(void)layoutSubviews方法,这个方法是设置系统的控件的,设置思想就是先遍历所有的空间,再判断这是什么控件,if ([v isKindOfClass:NSClassFromString(@"UIAlertButton")])这个可以判断是不是UIAlertButton的子类。

    接下来就只需要在ViewController里面创建这个类的对象就行了,再show就行了

    - (IBAction)showAlertView:(id)sender
    {
         CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"" message:@"白猪" delegate:nil cancelButtonTitle:@"" otherButtonTitles:@"很帅", nil];
        [alert show];
      

    然后附上效果图:

    有什么问题请留言,欢迎指正

  • 相关阅读:
    2018-12-25-dot-net-double-数组转-float-数组
    2018-12-25-dot-net-double-数组转-float-数组
    2019-10-24-dotnet-列表-Linq-的-Take-用法
    2019-10-24-dotnet-列表-Linq-的-Take-用法
    2018-8-10-C#-代码占用的空间
    2018-8-10-C#-代码占用的空间
    2018-4-29-C#-金额转中文大写
    2018-4-29-C#-金额转中文大写
    Java实现 LeetCode 630 课程表 III(大小堆)
    Java实现洛谷 P1072 Hankson 的趣味题
  • 原文地址:https://www.cnblogs.com/xiaobaizhu/p/UIAlertView.html
Copyright © 2011-2022 走看看