IOS 代码总结 UIImage UILabel UIButton

来源:互联网 发布:程序员真的那么累吗 编辑:程序博客网 时间:2024/06/07 04:05


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController




- (void)viewDidLoad

{

    [superviewDidLoad];

   self.title =@"通讯录";

    UIBarButtonItem *leftButton = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:selfaction:@selector(selectLeftAction:)];

    self.navigationItem.leftBarButtonItem =  leftButton;

    UIBarButtonItem *rightButton = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(selectRightAction:)];

    self.navigationItem.rightBarButtonItem = rightButton;

    

    UIButton *button = [[UIButtonalloc] init];

//     UIButton *button = [UIButton  buttonWithType:UIButtonTypeRoundedRect];

    button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect ];

    button.frame =CGRectMake(143, 43, 80, 40) ;

    button.backgroundColor = [UIColorredColor];

//    button.titleLabel.text = @"111";

    [button setTitle:@"点击"forState:UIControlStateNormal];

    [button setImage:[UIImageimageNamed:@"11"]forState:UIControlStateNormal];

    [button setTitleShadowColor:[UIColorredColor] forState:UIControlStateNormal];

    [button setBackgroundImage:[UIImageimageNamed:@"22"]forState:UIControlStateNormal];

    [button setBackgroundColor:[UIColorredColor]];

    [button setImage:[UIImageimageNamed:@"33"]forState:UIControlStateNormal];

//    上面几个方法都提到共同的参数 forState . 这个参数决定了标题、图像或其他属性将在何种状态下显现。你可以编程令按钮在那个状态变化

//    typedef NS_OPTIONS(NSUInteger, UIControlState) {

//        UIControlStateNormal       = 0,   常态

//        UIControlStateHighlighted  = 1 << 0,    高亮               // used when UIControl isHighlighted is set

//        UIControlStateDisabled     = 1 << 1,   禁用

//        UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)

//        UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use

//        UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use

//    };

    

    //默认情况下,在按钮被禁用时,图像会被化得深一点。要禁用这个功能,请将这个属性设置为NO

    button.adjustsImageWhenHighlighted =NO;

    //这个属性设置为yes,按钮按下时高亮

    button.showsTouchWhenHighlighted =YES;

    

    //button添加响应

    [button addTarget:selfaction:@selector(zoomInAction:)forControlEvents:UIControlEventTouchUpInside];

    

//addTarget:self 是链接到self,一般都这样设置

//action:@selector(alarmTimeDone:) 时间处理函数

//forControlEvents:UIControlEventTouchUpInside 控件事件处理的消息

    

    

    [self.viewaddSubview:button];

    

//    添加图片view

    NSString *_imageViewPath = [[NSBundlemainBundle] pathForResource:@"11"ofType:@"pbg"];

   UIImage *_imageViewImage = [UIImageimageWithContentsOfFile:_imageViewPath];

    self._imageView = [[[UIImageViewalloc] init]autorelease];

   self._imageView.image = _imageViewImage;

    //第二种

    UIImageView *imagview = [[[UIImageViewalloc] initWithFrame:CGRectMake(24, 12, 90, 60)]autorelease] ;

    [imagview setImage:[UIImageimageNamed:@"niutou.jpg"]];

//    第三种

    NSData *data = [NSDatadataWithContentsOfFile:_imageViewPath];

    UIImage *image2 = [UIImageimageWithData:data];

    [imagviewsetImage:image2];


    

    UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(15, 65, 70, 30)];

    //设置显示的文字

    label.text =@"label";

    //设置文字的背景色

    label.backgroundColor = [UIColorredColor];

    //设置文字的字体大小

    label.font = [UIFontboldSystemFontOfSize:20];

//    设置文字的颜色

    label.textColor = [UIColorblackColor];

//    设置文字的位置靠左 靠右居中

    label.textAlignment =UITextAlignmentCenter;

//    设置文字是否适应label的大小

    label.adjustsFontSizeToFitWidth =YES;

//    设置文字现实的行数

    label.numberOfLines = 2;

//    设置高亮

    label.highlighted =YES;

    label.highlightedTextColor = [UIColororangeColor];

    //设置阴影

    label.shadowColor = [UIColorredColor];

    label.shadowOffset =CGSizeMake(1.0,1.0);

    //设置是否能与用户交互

    label.userInteractionEnabled =YES;

    //设置label中的文字是否可变,默认值是yes

    label.enabled =NO;

    //设置文字过长的显示格式

    label.lineBreakMode =UILineBreakModeMiddleTruncation;

//    typedef enum

//    {

//        UILineBreakModeWordWrap = 0;

//        UILineBreakModeCharacterWrap,

//        UILineBreakModeClip, //截去多余部分

//        UILineBreakModeHeadTruncation, //截去头部

//        UILineBreakModeTailTruncation, //截去尾部

//        UILineBreakModeMiddleTruncation, //截去中间

//    }UILineBreakMode;

    

    //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为

    

    label.baselineAdjustment =UIBaselineAdjustmentNone;

    

    //  typedef enum {

    

    //      UIBaselineAdjustmentAlignBaselines,

    

    //      UIBaselineAdjustmentAlignCenters,

    

    //      UIBaselineAdjustmentNone,

    

    //  } UIBaselineAdjustment;

    

    [labelrelease];

    [self.viewaddSubview:label];

    

   }



//    //重写绘制行为

//    backgroundRectForBounds //指定背景边界

//    contentRectForBounds //指定内容边界

//    titleRectForContentRect //指定文字标题边界

//    imageRectForContentRect //指定按钮图像边界

//

//    -(CGRect) imageRectForContentRect:(CGRect)bounds{

//

//    }


-(void)zoomInAction:(id)sender

{

    UIAlertView *myAlertview=[[UIAlertViewalloc]initWithTitle:@"123"message:@"11"delegate:selfcancelButtonTitle:@"ok"otherButtonTitles: nil];

       [myAlertviewshow];

}


-(void) selectLeftAction:(id)sender

{

//    UIAlertView *alter = [[UIAlertView alloc] initWithFrame:CGRectMake(50, 50, 80, 80)];

//    alter.title = @"111";

   UIAlertView *alter = [[UIAlertViewalloc] initWithTitle:@"提示"message:@"你点击了左按钮" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil,nil];

    [altershow];

}


-(void) selectRightAction:(id)sender

{

    UIAlertView *alter = [[UIAlertViewalloc] initWithTitle:@"提示"message:@"你点击了右按钮" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil,nil];

    [altershow];

}

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end







原创粉丝点击