IOS 代码总结 UIImage UILabel UIButton
2013-10-14 20:15
267 查看
#import "ViewController.h"
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
self.title =@"通讯录";
UIBarButtonItem *leftButton = [[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(selectLeftAction:)];
self.navigationItem.leftBarButtonItem = leftButton;
UIBarButtonItem *rightButton = [[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(selectRightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
UIButton *button = [[UIButton
alloc] init];
// UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button = [UIButton
buttonWithType:UIButtonTypeRoundedRect ];
button.frame =
CGRectMake(143, 43, 80, 40) ;
button.backgroundColor = [UIColor
redColor];
// button.titleLabel.text = @"111";
[button setTitle:@"点击"
forState:UIControlStateNormal];
[button setImage:[UIImage
imageNamed:@"11"]
forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor
redColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage
imageNamed:@"22"]
forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor
redColor]];
[button setImage:[UIImage
imageNamed:@"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:self
action:@selector(zoomInAction:)
forControlEvents:UIControlEventTouchUpInside];
//addTarget:self 是链接到self,一般都这样设置
//action:@selector(alarmTimeDone:) 时间处理函数
//forControlEvents:UIControlEventTouchUpInside
控件事件处理的消息
[self.view
addSubview:button];
// 添加图片view
NSString *_imageViewPath = [[NSBundle
mainBundle] pathForResource:@"11"
ofType:@"pbg"];
UIImage *_imageViewImage = [UIImage
imageWithContentsOfFile:_imageViewPath];
self._imageView = [[[UIImageView
alloc] init]autorelease];
self._imageView.image = _imageViewImage;
//第二种
UIImageView *imagview = [[[UIImageView
alloc] initWithFrame:CGRectMake(24, 12, 90, 60)]
autorelease] ;
[imagview setImage:[UIImage
imageNamed:@"niutou.jpg"]];
// 第三种
NSData *data = [NSData
dataWithContentsOfFile:_imageViewPath];
UIImage *image2 = [UIImage
imageWithData:data];
[imagview
setImage:image2];
UILabel *label = [[UILabel
alloc] initWithFrame:CGRectMake(15, 65, 70, 30)];
//设置显示的文字
label.text =
@"label";
//设置文字的背景色
label.backgroundColor = [UIColor
redColor];
//设置文字的字体大小
label.font = [UIFont
boldSystemFontOfSize:20];
// 设置文字的颜色
label.textColor = [UIColor
blackColor];
// 设置文字的位置
靠左 靠右
居中
label.textAlignment =
UITextAlignmentCenter;
// 设置文字是否适应label的大小
label.adjustsFontSizeToFitWidth =
YES;
// 设置文字现实的行数
label.numberOfLines = 2;
// 设置高亮
label.highlighted =
YES;
label.highlightedTextColor = [UIColor
orangeColor];
//设置阴影
label.shadowColor = [UIColor
redColor];
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;
[label
release];
[self.view
addSubview:label];
}
// //重写绘制行为
// backgroundRectForBounds //指定背景边界
// contentRectForBounds //指定内容边界
// titleRectForContentRect //指定文字标题边界
// imageRectForContentRect //指定按钮图像边界
//
// -(CGRect) imageRectForContentRect:(CGRect)bounds{
//
// }
-(void)zoomInAction:(id)sender
{
UIAlertView *myAlertview=[[UIAlertView
alloc]initWithTitle:@"123"
message:@"11"
delegate:self
cancelButtonTitle:@"ok"
otherButtonTitles: nil];
[myAlertview
show];
}
-(void) selectLeftAction:(id)sender
{
// UIAlertView *alter = [[UIAlertView alloc] initWithFrame:CGRectMake(50, 50, 80, 80)];
// alter.title = @"111";
UIAlertView *alter = [[UIAlertView
alloc] initWithTitle:@"提示"
message:@"你点击了左按钮" delegate:self
cancelButtonTitle:@"确定" otherButtonTitles:nil,
nil];
[alter
show];
}
-(void) selectRightAction:(id)sender
{
UIAlertView *alter = [[UIAlertView
alloc] initWithTitle:@"提示"
message:@"你点击了右按钮" delegate:self
cancelButtonTitle:@"确定" otherButtonTitles:nil,
nil];
[alter
show];
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
self.title =@"通讯录";
UIBarButtonItem *leftButton = [[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(selectLeftAction:)];
self.navigationItem.leftBarButtonItem = leftButton;
UIBarButtonItem *rightButton = [[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(selectRightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
UIButton *button = [[UIButton
alloc] init];
// UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button = [UIButton
buttonWithType:UIButtonTypeRoundedRect ];
button.frame =
CGRectMake(143, 43, 80, 40) ;
button.backgroundColor = [UIColor
redColor];
// button.titleLabel.text = @"111";
[button setTitle:@"点击"
forState:UIControlStateNormal];
[button setImage:[UIImage
imageNamed:@"11"]
forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor
redColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage
imageNamed:@"22"]
forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor
redColor]];
[button setImage:[UIImage
imageNamed:@"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:self
action:@selector(zoomInAction:)
forControlEvents:UIControlEventTouchUpInside];
//addTarget:self 是链接到self,一般都这样设置
//action:@selector(alarmTimeDone:) 时间处理函数
//forControlEvents:UIControlEventTouchUpInside
控件事件处理的消息
[self.view
addSubview:button];
// 添加图片view
NSString *_imageViewPath = [[NSBundle
mainBundle] pathForResource:@"11"
ofType:@"pbg"];
UIImage *_imageViewImage = [UIImage
imageWithContentsOfFile:_imageViewPath];
self._imageView = [[[UIImageView
alloc] init]autorelease];
self._imageView.image = _imageViewImage;
//第二种
UIImageView *imagview = [[[UIImageView
alloc] initWithFrame:CGRectMake(24, 12, 90, 60)]
autorelease] ;
[imagview setImage:[UIImage
imageNamed:@"niutou.jpg"]];
// 第三种
NSData *data = [NSData
dataWithContentsOfFile:_imageViewPath];
UIImage *image2 = [UIImage
imageWithData:data];
[imagview
setImage:image2];
UILabel *label = [[UILabel
alloc] initWithFrame:CGRectMake(15, 65, 70, 30)];
//设置显示的文字
label.text =
@"label";
//设置文字的背景色
label.backgroundColor = [UIColor
redColor];
//设置文字的字体大小
label.font = [UIFont
boldSystemFontOfSize:20];
// 设置文字的颜色
label.textColor = [UIColor
blackColor];
// 设置文字的位置
靠左 靠右
居中
label.textAlignment =
UITextAlignmentCenter;
// 设置文字是否适应label的大小
label.adjustsFontSizeToFitWidth =
YES;
// 设置文字现实的行数
label.numberOfLines = 2;
// 设置高亮
label.highlighted =
YES;
label.highlightedTextColor = [UIColor
orangeColor];
//设置阴影
label.shadowColor = [UIColor
redColor];
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;
[label
release];
[self.view
addSubview:label];
}
// //重写绘制行为
// backgroundRectForBounds //指定背景边界
// contentRectForBounds //指定内容边界
// titleRectForContentRect //指定文字标题边界
// imageRectForContentRect //指定按钮图像边界
//
// -(CGRect) imageRectForContentRect:(CGRect)bounds{
//
// }
-(void)zoomInAction:(id)sender
{
UIAlertView *myAlertview=[[UIAlertView
alloc]initWithTitle:@"123"
message:@"11"
delegate:self
cancelButtonTitle:@"ok"
otherButtonTitles: nil];
[myAlertview
show];
}
-(void) selectLeftAction:(id)sender
{
// UIAlertView *alter = [[UIAlertView alloc] initWithFrame:CGRectMake(50, 50, 80, 80)];
// alter.title = @"111";
UIAlertView *alter = [[UIAlertView
alloc] initWithTitle:@"提示"
message:@"你点击了左按钮" delegate:self
cancelButtonTitle:@"确定" otherButtonTitles:nil,
nil];
[alter
show];
}
-(void) selectRightAction:(id)sender
{
UIAlertView *alter = [[UIAlertView
alloc] initWithTitle:@"提示"
message:@"你点击了右按钮" delegate:self
cancelButtonTitle:@"确定" otherButtonTitles:nil,
nil];
[alter
show];
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- iOS开发总结之代码规范
- iOS按钮UIButton常用属性总结
- ios-AutoLayout(自动布局代码控制)简单总结
- iOS - UIButton 开发总结
- IOS多线程 总结 -------------核心代码(GCD)
- IOS 代码风格习惯 总结1
- 关于IOS开发的一些有用的代码片段(别人总结和自己的)
- IOS里写代码要注意的总结
- IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别(附代码)
- ios开发autolayout之代码实现Autolayout使用总结
- IOS UIButton 代码创建
- IOS 代码控制UIButton
- ios-AutoLayout(自动布局代码控制)简单总结
- iOS开发总结之代码规范
- IOS总结_实现UIButton的图文混排(二)
- IOS UIButton的属性总结
- iOS代码添加UIButton
- iOS 代码实践总结
- ios-AutoLayout(自动布局代码控制)简单总结
- IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl