热门IT资讯网

UI中控件的应用

发表于:2024-11-25 作者:热门IT资讯网编辑
编辑最后更新 2024年11月25日,UI初级中的控件是UI学习的最基本的应用,下面是一些最基础的控件的应用UILabel#pragma mark -UILabel- (void)_initLabel{UILabel *textLable

UI初级中的控件是UI学习的最基本的应用,下面是一些最基础的控件的应用

UILabel

#pragma mark -UILabel

- (void)_initLabel

{

UILabel *textLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 150, 250)];

textLable.backgroundColor = [UIColor grayColor];

//设置文本内容

textLable.text = @"good morning hehehehehe good morning hehehehehe";

//设置字体, systemFont使用系统的字体,大小10

textLable.font = [UIFont systemFontOfSize:16];

//设置粗体

textLable.font = [UIFont boldSystemFontOfSize:16];

//字体类 UIFont

NSArray *familyNames = [UIFont familyNames];

NSLog(@"familyNames is %@", familyNames);

textLable.font = [UIFont fontWithName:@"Zapf Dingbats" size:16];

//设置字体颜色

textLable.textColor = [UIColor orangeColor];

//设置文本对齐方式

textLable.textAlignment = NSTextAlignmentCenter;

//设置当前的显示行数,默认是1, 如果设为0,是自动换行

textLable.numberOfLines = 0;

//自动根据文本调整宽度和高度

[textLable sizeToFit];

// NSLog(@"textLabel is %@", textLable);

[self.window addSubview:textLable];

}

UIButton

#pragma mark -UIButton

- (void)_initButton

{

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(10, 180, 90, 44);

button.backgroundColor = [UIColor greenColor];

//设置显示标题, 标题总是需要跟状态绑定到一起的

// button.titleLabel.text = @"hehe"; //错误,不能这样设置title

/*

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

};

*/

[button setTitle:@"hehe" forState:UIControlStateNormal];

//设置高亮状态下的title

// [button setTitle:@"haha" forState:UIControlStateHighlighted];

//设置选中状态下的title

// [button setTitle:@"hihi" forState:UIControlStateSelected];

//设置按钮是否选中

// button.selected = true;

//设置标题的字体

button.titleLabel.font = [UIFont boldSystemFontOfSize:20];

//设置标题的颜色

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

// [button setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];

0