码迷,mamicode.com
首页 > 其他好文 > 详细

使用代理和block写一个alertView

时间:2015-06-07 12:19:00      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

代理:

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:
@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

 
 
 
 

使用代理和block写一个alertView

标签:

原文地址:http://www.cnblogs.com/renlipeng/p/4558119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!