首页 > 代码库 > UILabel,UITextField和UIButton使用简示

UILabel,UITextField和UIButton使用简示

关于题中三个控件的简要使用例子。

代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 设置视图背景色
    [self.view setBackgroundColor:[UIColor colorWithRed:51/255.0 green:204/255.0 blue:255/255.0 alpha:1]];
    
    // 控件高和宽
    float height = 48;
    float width = self.view.frame.size.width*2/3;
    
    // 控件和view的边距
    float xEdge = width/4;
    float yEdge = 5;
    float nextY = 0;
    
    
    /* UILabel example */
    UILabel* labelLogo = [[UILabel alloc] initWithFrame:CGRectMake(xEdge, 160, width, height)];
    labelLogo.text = @"Google+";
    labelLogo.textAlignment = NSTextAlignmentCenter;
    
    // 随机设置一种字体
    NSArray* font = [UIFont familyNames];
    labelLogo.font = [UIFont fontWithName:font[arc4random()%font.count] size:40];
    
#if 0
    NSLog(@"System Font List:");
    for(int i=0; i<font.count; i++)
    {
        NSLog(@"%@", font[i]);
    }
    // 设置高亮
    label1.highlighted = YES;
    label1.highlightedTextColor = [UIColor redColor];
    
    // 根据文字个数,字体大小自适应
    label1.adjustsFontSizeToFitWidth=YES;
    // 文字自适应的基准线
    label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
#endif
    
    // 颜色设置
    labelLogo.textColor = [UIColor blueColor];
    labelLogo.backgroundColor = [UIColor clearColor];

    // 设置阴影
    labelLogo.shadowColor = [UIColor grayColor];
    //设置阴影偏移值,需要CGSizeMake值,第一个表示左右偏移,>0向右;第二个表示上下偏移,>0向下
    labelLogo.shadowOffset = CGSizeMake(2, 3);

    // 添加至view
    [self.view addSubview:labelLogo];
    
    // 圆角
    float cornerRadius = 10;
    
    // 两个文本框
    nextY = labelLogo.frame.origin.y + labelLogo.frame.size.height;
    nextY += 10*yEdge;
    UITextField* textAccount = [[UITextField alloc] initWithFrame:CGRectMake(xEdge, nextY, width, height)];
    // 文本提示信息
    textAccount.placeholder = @"email/account";
    textAccount.backgroundColor = [UIColor whiteColor];
    textAccount.layer.cornerRadius = cornerRadius;
    [self.view addSubview:textAccount];
    
    nextY = textAccount.frame.origin.y + textAccount.frame.size.height;
    nextY += yEdge;
    UITextField* textPassword = [[UITextField alloc] initWithFrame:CGRectMake(xEdge, nextY, width, height)];
    textPassword.placeholder = @"password";
    textPassword.backgroundColor = [UIColor whiteColor];
    textPassword.layer.cornerRadius = cornerRadius;
    textPassword.secureTextEntry = YES;
    [self.view addSubview:textPassword];
    
    // 登录按钮
    nextY = textPassword.frame.origin.y + textPassword.frame.size.height;
    nextY += 10*yEdge;
    UIButton* buttonLogin = [[UIButton alloc] initWithFrame:CGRectMake(xEdge, nextY, width, height)];
    [buttonLogin setTitle:@"Login" forState:UIControlStateNormal];
    [buttonLogin setBackgroundColor:[UIColor colorWithRed:51/255.0 green:102/255.0 blue:255/255.0 alpha:1]];
    buttonLogin.layer.cornerRadius = cornerRadius;
    // 添加按钮点击响应事件
    [buttonLogin addTarget:self action:@selector(onClickLogin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonLogin];
}


运行结果




工程代码链接


UILabel,UITextField和UIButton使用简示