您的位置:首页 > 移动开发 > IOS开发

IOS中对控件的操作

2013-12-11 19:20 253 查看
将一个图片作为视图的背景代码如下:
- (void)addImageView
{

//找到图片
   
UIImage *pImage=[UIImage
imageNamed:@"sea.png"];

    //根据内容图片创建ImageView
   
self.pImageView = [[UIImageView
alloc] initWithImage:pImage];

    //将ImageView放到父视图中

    [self.view
insertSubview:self.pImageView
atIndex:0];
}

对Label的操作代码如下
- (void)addLabel
{

//创建一个Label
   
UILabel *pLabel=[[UILabel
alloc] initWithFrame:CGRectMake(10,
10, 120,
50)];
   
//内容

    pLabel.text=@"HelloWorld\nSecondLine";

    //设置字体和大小

    pLabel.textAlignment=NSTextAlignmentCenter;

    //字体颜色
    pLabel.textColor=[UIColor
redColor];

    //显示行数
    pLabel.numberOfLines=
2;

    //阴影颜色

    pLabel.shadowColor=[UIColor
blackColor];

    //阴影尺寸
    pLabel.shadowOffset =
CGSizeMake(2.0,
1.0);

    //设置label的背景为透明色

    pLabel.backgroundColor=[UIColor
clearColor];
    [self.view
addSubview:pLabel];
    [pLabel
release];
}

文本框的操作代码如下:

- (void)addTextField
{

//创建TextField
   
UITextField *pTextField=[[UITextField
alloc] initWithFrame:CGRectMake(10,
116, 200,
31)];

    //设置边框样式

    pTextField.borderStyle =
UITextBorderStyleRoundedRect;

    //设置字体
    pTextField.font = [UIFont
systemFontOfSize:18.0];

    //根据宽度改变字体

    pTextField.adjustsFontSizeToFitWidth =
YES;

    //最小字体
    pTextField.minimumFontSize =
2.0;

    //清除按钮的样式

    pTextField.clearButtonMode =
UITextFieldViewModeWhileEditing;

   //弹出键盘的样式

    pTextField.keyboardType =
UIKeyboardTypeDefault;

    //设置使用自动更正功能

    pTextField.autocorrectionType=
UITextAutocorrectionTypeNo;

    //设置键盘自动大小写的属性

    pTextField.autocapitalizationType =
UITextAutocapitalizationTypeNone;

    //设置返回按钮的类型
    pTextField.returnKeyType =
UIReturnKeyDone;

    //设置是否支持密码文本显示
    pTextField.secureTextEntry =
YES;

    //设置委托
    pTextField.delegate =
self;
    [self.view
addSubview:pTextField];
    [pTextField
release];
}

按钮的操作代码如下:

//控制控件
- (void)addButton
{

//创建一个按钮

    UIButton *pBtn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];

    //设置区域
    [pBtn
setFrame:CGRectMake(10,
70, 100,
40)];

    [pBtn setTitle:@"Normal"
forState:UIControlStateNormal];

    [pBtn setTitle:@"HighLight"
forState:UIControlStateHighlighted];

    //允许显示高亮

    pBtn.showsTouchWhenHighlighted =
YES;

    [pBtn addTarget:self
action:@selector(buttonDown:)
forControlEvents:UIControlEventTouchDown];

    [pBtn addTarget:self
action:@selector(buttonRelease:)
forControlEvents:UIControlEventTouchUpInside];
    [self.view
addSubview:pBtn];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: