zoukankan      html  css  js  c++  java
  • 使用代理和block写一个alertView

    代理:

    MyAlertView.h:
    @property (nonatomic,assign)id      delegate;
    
    @protocol MyAlertViewDelegate <NSObject>
    
    - (void)myAlertView:(MyAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
    
    @end
    MyAlertView.m:
    - (void)buttonClick:(id)sender { UIButton *button = (UIButton *)sender; int index = (int)button.tag - 10; if ([self.delegate respondsToSelector:@selector(myAlertView:clickedButtonAtIndex:)]) { [self.delegate myAlertView:self clickedButtonAtIndex:index]; } [self disappear]; }
    调用处ViewController:
    - (void)myAlertView:(MyAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"buttonIndex:%d",buttonIndex); }

    使用block:

    MyAlertView.h:
    
    //使用copy 而不是retain ,因为block 变量是声明在栈上的,retain之后还是在栈上,方法结束,栈的内存就被系统自动释放,copy 可以把一个block 变量从栈上拷贝到堆上,堆上的内存由引用计数管理,所以我们就能自己决定block的释放。
    @property (nonatomic,copy)void (^buttonClick)(int);
    MyAlertView.m:
    - (void)buttonClick:(id)sender { UIButton *button = (UIButton *)sender; int index = (int)button.tag - 10; self.buttonClick(index); [self disappear]; }
    调用处ViewController:
    alertView.buttonClick = ^(int index){ NSLog(@"buttonIndex:%d",buttonIndex); };

    UILabel类别自适应高度

    #import <UIKit/UIKit.h>
    
    @interface UILabel (AutoFitSize)
    
    +(UILabel *)getInfoLabel:(UILabel *)label withText:(NSString *)ktext withFont:(UIFont *)kfont withtosize:(CGRect)krect withBackGroundColor:(UIColor *)kbackgroundColor;
    
    @end
    #import "UILabel+AutoFitSize.h"
    
    //判断是否是IOS7以上的系统(包含IOS7)
    #define bIos7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0?YES : NO)
    
    @implementation UILabel (AutoFitSize)
    
    #if __IPHONE_OS_VERSION_MAX_ALLOWED < 70000
    
    +(UILabel *)getInfoLabel:(UILabel *)label withText:(NSString *)ktext withFont:(UIFont *)kfont withtosize:(CGRect)krect withBackGroundColor:(UIColor *)kbackgroundColor
    {
        label.font=kfont;
        label.text = ktext;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor=kbackgroundColor;
    
        label.numberOfLines=0;
        label.lineBreakMode=NSLineBreakByTruncatingTail;
        //内容显示  高度自适应
        CGSize sizeToFit =[ktext sizeWithFont:font constrainedToSize:CGSizeMake(krect.size.width,10000)lineBreakMode:NSLineBreakByWordWrapping];
        label.frame=CGRectMake(krect.origin.x,krect.origin.y,sizeToFit.width, sizeToFit.height);;
        return label;
    }
    
    #else
    
    +(UILabel *)getInfoLabel:(UILabel *)label withText:(NSString *)ktext withFont:(UIFont *)kfont withtosize:(CGRect)krect withBackGroundColor:(UIColor *)kbackgroundColor
    {
        label.text = ktext;
        label.font = kfont;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = kbackgroundColor;
        
        label.numberOfLines =0;
        label.lineBreakMode =NSLineBreakByTruncatingTail ;
        //高度估计文本大概要显示几行,宽度根据需求自己定义。 MAXFLOAT 可以算出具体要多高
        CGSize size =CGSizeMake(krect.size.width,10000);
        NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:kfont,NSFontAttributeName,nil];
        //ios7方法,获取文本需要的size,限制宽度
        CGSize  actualsize =[ktext boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
        label.frame=CGRectMake(krect.origin.x,krect.origin.y,actualsize.width, actualsize.height);
        
        return label;
    }
    
    #endif
    
    @end

    代码地址:http://pan.baidu.com/s/1hqAKp3u

     
     
     
     
     
    原创文章,转载请声明出处!
  • 相关阅读:
    冲刺第十三天
    冲刺第十二天
    冲刺第十一天
    Android Studio三种运行方法
    第十三周学习进度
    冲刺第一阶段意见评论
    第十二周学习进度
    冲刺第十天
    大二暑假周总结(五)
    大二暑假周总结(四)
  • 原文地址:https://www.cnblogs.com/renlipeng/p/4558119.html
Copyright © 2011-2022 走看看