1.UIImageView 的 contentMode
UIImageView 的 contentMode 属性用来设置图片的显示方式,如居中,局左,是否缩放等,contentMode 是 UIViewContentMode(枚举类型) 的属性,主要有下面这些样式可供选择
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill, //会导致图片变形
UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent -->保证图片比例不变的最大显示图片,可能会有留白部分
UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped. --> 保证图片比例不变的情况下,填充整个 imageView 视图,可能只有部分图片显示
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};
|
以上这些模式,带有 scale 的,当图片尺寸超过 imageView 的尺寸( frame)时,可能造成只有部分图片 内容会显示在 imageView 中,可以通过剪裁命令剪裁掉不在 imageView 视图之内的图片内容self.myImageView.clipsToBounds = YES;
2.Textfield 的 leftViewMode 和 rightViewMode
TextField 是自带左边视图和右边视图的,可以用来实现一些特殊的显示效果,默认是隐藏的,
1
2
3
4
5
6
|
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
};
|
下面代码演示了如何使用
1
2
3
4
5
6
7
8
9
10
11
12
|
UIImageView *leftN = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon"]];
UIImageView *leftP = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lock"]];
/** 给 textField 添加 左视图 小人 */
leftN.frame = CGRectMake(0, 0, 55, 20);
leftN.contentMode = UIViewContentModeCenter;//contentMode
self.userNameField.leftView = leftN;//
self.userNameField.leftViewMode = UITextFieldViewModeAlways;
/** 给 textField 添加 左视图 小锁 */
leftP.frame = CGRectMake(0, 0, 55, 20);
leftP.contentMode = UIViewContentModeCenter;//contentMode
self.userPasswordField.leftView = leftP;//
self.userPasswordField.leftViewMode = UITextFieldViewModeAlways;
|
代码最终效果