美文网首页IosiOS学习笔记iOS Developer
[关于UIView.UILabel.UIButton.UIIma

[关于UIView.UILabel.UIButton.UIIma

作者: Listen_黎森 | 来源:发表于2016-07-16 20:17 被阅读182次

一. UIView

/* UIView 的创建 */
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 30)];

/* 设置UIView背景颜色 */
view.backgroundColor = [UIColor cyanColor];

/* 使用取色器自定义RBG颜色的方法 */
view.backgroundColor = [UIColor colorWithRed:255 / 255 green:102 / 255 blue:255 / 255 alpha:1];//找到你想要的颜色RBG值 除255 放入相应位置 如图选择了一块粉色区域

选中一块粉色
参照RGB图表

/* 将UIView对象添加到父视图上 */
[self.view addSubview:view];
结果如下图


显示结果

/* 添加/删除子视图方法总览 */
[addSubview:]//添加子视图
[insertSubview: atIndex:]//在指定的index处插入子视图
[insertSubview: aboveSubview:]//在指定的视图上添加子视图
[insertSubview: belowSubview:]//在指定的视图下添加子视图
[bringSubviewToFront:]//把指定的子视图移动到最前面
[sendSubviewToBack:]//把指定的子视图移动到最后面
[exchangeSubviewAtIndex: withSubviewAtIndex:]//交换两个指定位置的子视图
removeFromSuperview//把子视图从父视图上移除

/* 关于UIView的属性 */
1.backgroundColor 背景颜色
2.frame 位置及尺寸 以父视图左上角为原点
3.center 中心点
4.hidden 布尔值 YES隐藏 No显示
5.alpha 透明度
6.superview 本视图的父视图
7.subviews 本视图的所有子视图 是一个数组
8.tag 标记
9.contentMode 内容显示模式 拉伸自适应
10.layer
11.bounds 位置及尺寸 以自己的左上角为原点 影响其子视图的位置布局

/* UIView对象的圆角设置 */
view.layer.masksToBounds = YES;
view.layer.cornerRadius = 5;//设置圆角半径
view.layer.borderColor = [[UIColor cyanColor] CGColor];//设置边框颜色
view.layer.borderWidth =3;//设置边框宽度
结果 如下图

显示结果

二.UILabel

/* UILabel的属性 */
1.text 文本内容
2.frame 位置大小
3.textColor 字的颜色
4.textAlignment 对齐方式
 (1)NSTextAlignmentLeft 左对齐
 (2)NSTextAlignmentCenter 居中
 (3)NSTextAlignmentRight 右对齐
5.numberOfLines 行数 
6.lineBreakMode 断行模式
 (1)NSLineBreakByWordWrapping //以空格为边界,保留单词
 (2)NSLineBreakByCharWrapping //保留整个字符
 (3)NSLineBreakByClipping //简单剪裁,到边界为止
 (4)NSLineBreakByTruncatingHead //按照"……文字"显示
 (5)NSLineBreakByTruncatingTail //按照"文字……文字"显示
 (6)NSLineBreakByTruncatingMiddle //按照"文字……"显示
7.layer 层级
8.font 字体大小
9.shadowColor 阴影颜色
10.shadowOffset 阴影偏移量 是CGSize 类型
11.alpha 透明度

/* UILabel的创建 */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 120, 100, 40)];
label.backgroundColor = [UIColor yellowColor];//背景颜色
label.text = @"你好帅";//添加文本内容
[self.view addSubview:label];//添加到父视图上
label.textColor = [UIColor blueColor];//字体颜色设置
label.textAlignment = 1;//对齐方式 居中
label.layer.masksToBounds = YES;
label.layer.cornerRadius = 5;//圆角半径
label.shadowColor = [UIColor redColor];//阴影颜色
label.shadowOffset = CGSizeMake(3, 3);//阴影偏移量
label.layer.borderWidth = 2;//边框宽度

结果 如下图



若我们这样设置

label.text = @"你说什么 哦豁.what 嘻嘻我怎么听不到";
label.numberOfLines = 0;
label.lineBreakMode = 2;
label.font = [UIFont systemFontOfSize:12];

就会变成下图 保留了完整的字符


三. UIButton

/* UIButton的属性 */
1.backgroundColor 背景颜色
2.frame 位置大小
3.titleLabel 标题
4.layer 层级
5.alpha 透明度

/* button初始化 */
//有2种方式
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];// button上的字可以更改颜色
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];//button上的字颜色为白色 且不能更改

外观控制属性
[setImage: forState:] 添加图片
[setBackgroundImage: forState:] 添加背景图片
[setTitle: forState:] 添加标题
[setTintColor:] 定义标题字体颜色
[setTitleShadowColor: forState:] 标题字体阴影颜色

在这里简单定义下属性
[button setTitle:@"注册" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button1 setTitle:@"登录" forState:UIControlStateNormal];
如下图

当然 你可以把它设置的很漂亮
如 透明底色带边框的button


这里小编是这么写的
//注册

UIButton *buttonOfSignIn = [UIButton buttonWithType:UIButtonTypeSystem];
buttonOfSignIn.frame = CGRectMake(10, 460, 355, 40);
buttonOfSignIn.backgroundColor = [UIColor colorWithRed:0.18 green:0.8 blue:0.58 alpha:1];
[imageView addSubview:buttonOfSignIn];
[buttonOfSignIn setTitle:@"注册" forState:UIControlStateNormal];
[buttonOfSignIn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[buttonOfSignIn addTarget:self action:@selector(handleSignIn:) forControlEvents:UIControlEventTouchUpInside];
//返回登录
UIView *viewOfGetBack = [[UIView alloc] initWithFrame:CGRectMake(10, 530, 350, 40)];
viewOfGetBack.backgroundColor = [UIColor colorWithRed:0.18 green:0.8 blue:0.58 alpha:1];
viewOfGetBack.alpha = 0.1;
[imageView addSubview:viewOfGetBack];
UIButton *buttonOfGetBack = [UIButton buttonWithType:UIButtonTypeSystem];
buttonOfGetBack.frame = CGRectMake(10, 530, 350, 40);
[buttonOfGetBack setTitle:@"返回登录" forState:UIControlStateNormal];buttonOfGetBack.layer.masksToBounds = YES;
buttonOfGetBack.layer.borderWidth = 1;
buttonOfGetBack.layer.borderColor = [[UIColor colorWithRed:0.18 green:0.8 blue:0.58 alpha:1] CGColor];
buttonOfGetBack.titleLabel.font = [UIFont boldSystemFontOfSize:18];
[buttonOfGetBack setTitleColor:[UIColor colorWithRed:0.2 green:0.8 blue:0.58 alpha:1] forState:UIControlStateNormal];
[buttonOfGetBack addTarget:self action:@selector(handleGetBack:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:buttonOfGetBack];

四.UIImageView.UIImage
UIImageView是用于显示图片的类 UIImage是图片类
使用ImageView设置动态图

//初始化图片数组
NSMutableArray *array = [NSMutableArray array];
for (int i = 1; i < 19; i++) {
    NSString *flowerName = [NSString stringWithFormat:@"flower%d.tiff", i];
    [UIImage imageNamed:flowerName];
    UIImage *flowerImage = [UIImage imageNamed:flowerName];
    [array addObject:flowerImage];//将图片用for循环放入数组中
}
imageView.animationImages = array;设置动态图片数组
imageView.animationDuration = 0.5;设置播放一组动态图片的时间
imageView.animationRepeatCount = 0;设置重复次数 0为无限重复
[_window addSubview:imageView];
[imageView startAnimating];开始动画

这样向日葵就会左右摇动啦QVQ

//UIImage图片防止渲染的方法:
[UIImage imageNamed:@"1"] imageWithRenderingModem:UIImageRenderingModeAlwaysOriginall];

五.UITextField
UITextField 输入框 是控制文本输入和显示的控件

textfield属性:
1.text 文本内容
2.textColor 文本字颜色
3.textAlignment 对齐方式
4.font 字大小
5.frame 位置大小
6.placeholder 占位符
7.leftView 左视图
8.rightView 右视图
9.borderStyle 边框风格
  (1)UITextBorderStyleNone默认无                             
  (2)UITextBorderStyleRoundedRect圆角   
  (3)UITextBorderStyleLine边缘线  
  (4)UITextBorderStyleBezel贝塞尔
10.leftViewMode 左视图显示模式
11.rightViewMode 右视图显示模式
12.clearButtonMode 清除按钮模式
13.inputAccessoryView 输入视图上方的辅助视图
14.inputView 自定义输入键盘
15.returnKeyType 键盘右下角return 按键类型
16.keyboardType 弹出的键盘类型
17.secureTextEntry 是否密文输入

//点击return 回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField canResignFirstResponder];//
    [textField endEditing:YES];
    return YES;
}

//设置placeholder的字体颜色
textOfUser.placeholder = @"请输入已验证手机号码或邮箱";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];//新建可变字典
dict[NSForegroundColorAttributeName] = [UIColor lightGrayColor];//设置颜色属性
NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:textOfUser.placeholder attributes:dict];
[textOfUser setAttributedPlaceholder:attribute];

默认占位符文字是黑色的 这里改成了灰色~


UITextField 协议
签订协议后 可以在其声明周期中进行代码编写
生命周期:
//开始编辑前 判断能否编辑

  • (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
    return YES;
    }
    已经开始编辑时
  • (void)textFieldDidBeginEditing:(UITextField *)textField
    {
    }
    判断是否结束编辑
  • (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
    return YES;
    }
    点击return回收键盘
  • (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
    [textField endEditing:YES];
    return YES;
    }

相关文章

  • [关于UIView.UILabel.UIButton.UIIma

    一. UIView /* UIView 的创建 */UIView *view = [[UIView alloc] ...

  • 关于关于关于

    他们爱他们自己,不爱你 他们爱你是他们的母亲妻子女儿姐妹 他们不爱你 直到你死的时候,爱才产生,与遗忘同时 那也不...

  • 光明人生

    关于出生 关于成长 关于求学 关于青春期 关于恋爱 关于择业 关于婚姻 关于养生 关于家庭 关于人际 关于教子 关...

  • 「梦雅的简动力」打卡计时65天

    * 关于人生 * 关于梦想 * 关于方向 * 关于创业 * 关于投资 * 关于成败 * 关于个性 * 关于高度 *...

  • 关于

    关于两个人? 关于100步? 关于回头? 关于深情? 关于家庭? 关于孩子? 关于成长? 关于伤痛? 关于怀抱? ...

  • 2017新手妈妈年终总结

    关于购物 关于体重 关于减肥 关于纪念日 关于生活态度 关于上班 关于职场晋升加薪 关于睡眠 关于抱孩子 关于发型...

  • 2018-11-28

    关于流浪、关于随心、关于自由、关于世俗、关于规则、关于坦诚、关于真我、关于好奇心、关于对这整个世界的态度、关于整个...

  • 一首歌的时间

    认真的 想理出点思绪 关于今天关于明天 关于工作关于梦想 关于冬天关于夜晚 关于阳光关于浪花 关于木马关于窗花 关...

  • 最近的各种关于

    关于运动,关于中文阅读,关于英文听力,关于口算。 关于专注,关于目标,关于举家迁移。 关于对正确的过于执着,关于对...

  • 『关于』

    关于雨落,关于天晴; 关于入夜,关于天明; 关于齐眉,关于耳鬓; 关于缘定,关于今生。 (早安~诸君!)

网友评论

    本文标题:[关于UIView.UILabel.UIButton.UIIma

    本文链接:https://www.haomeiwen.com/subject/cmlujttx.html