iOS基础之OC简单控件知识了解

来源:互联网 发布:电影撞车影评 知乎 编辑:程序博客网 时间:2024/06/14 16:51

一.UIView属性

1.alpha

设置视图的透明度.默认为1.

    // 完全透明

    view.alpha =0;

    // 不透明

   view.alpha =1;

2.clipsToBounds

 // 默认是NO,当设置为yes时,超出当前视图的尺寸的内容和子视图不会显示。

  view.clipsToBounds =YES;

3.hidden

// 默认是NO,当设置为yes,视图就看不见了。

view.hidden =YES;

 

4.userInteractionEnabled

    // 默认为YES,如果设置为Noview就不能和用户交互了。(即不能响应事件)

view.userInteractionEnabled =NO;

5. tag

    // 默认为0,用来标记视图的

view.tag =0;

5. exclusiveTouch

默认为No

exclusiveTouch的意义在于:如果前设置了exclusiveTouchUIView是整个触摸事件的第一响者,那么到你所有的手指离屏幕前其他的UIView是无法接受到整个事件周期所有的触摸事件。

6.CGRect frame

1> 表示控件的位置和尺寸(以父控件的左上角为坐标原点(0,0))

2> 修改这个属性,可以调整控件的位置和尺寸

 

7.CGPoint center

1> 表示控件的中点(以父控件的左上角为坐标原点)

2> 修改这个属性,可以调整控件的位置

 

8.CGRect bounds

1> 表示控件的位置和尺寸(以自己的左上角位坐标原点,位置永远是(0,0))

2> 修改这个属性,只能调整控件的尺寸

 

9.CGAffineTransform transform

1> 表示控件的形变状态(旋转角度、缩放比例)

2> 创建CGAffineTransform的函数

*CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)

创建一个x、y方向的缩放比例分别为sx、sy的形变值

 

*CGAffineTransformMakeRotation(CGFloat angle)

创建一个旋转角度为angle的形变值

 

*CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy)

在形变值t的基础上,再进行缩放,x、y方向的缩放比例分别为sx、sy,然后返回一个新的形变值

 

*CGAffineTransformRotate(CGAffineTransform t, CGFloat angle)

在形变值t的基础上,再进行旋转,旋转角度为angle,然后返回一个新的形变值

10.superview

返回当前视图的父视图。(只读)

11.window

返回当前视图的窗口。(窗口)

 

获取根视图的superviewwindow时,需要注意,在viewdidload中是获取不到的,viewdidload只是视图加载完成,并没有添加到窗口中,因此需要在viewDidAppear方法中才能获取到。那时候视图才被添加到窗口中。

- (void)viewDidLoad

{

    [super viewDidLoad];

    NSLog(@"%@",self.view.superview); // 没有值

    NSLog(@"%@",self.view.window); // 没有值

}

- (void)viewDidAppear:(BOOL)animated

{

    NSLog(@"%@",self.view.superview); // 有值

    NSLog(@"%@",self.view.window);  // 有值

}

 

12.autoresizesSubviews

默认为YES,表示当父视图尺寸改变时,子视图也会随着改变。

13.autoresizingMask

默认为UIViewAutoresizingNone,不会自动伸缩。

14.contentMode

设置内容模式。

UIViewContentModeScaleToFill  不按照原宽高比例(长和宽不等比例增长)以任意比例填充。这样视图不会有空白,且内容可以全部显示。

UIViewContentModeAspectToFill  按照原长宽比例填充,不完全显示全部内容。这样内容可能溢出,但整个视图不会留有空白。

UIViewContentModeAspectToFit   按照原长宽比例(长和宽等比例增长),完全显示全部内容。这样容易照成左右或者上下留有空白。

15.backgroundColor

默认是nil。

// 设置背景颜色为红色

self.view.backgroundColor = [UIColorredColor];

16.UIView常用添加子视图方法

1.  //加一个视到一个视里面  

2.  addSubview:  

3.  //一个视移到前面  

4.  bringSubviewToFront:  

5.  //一个视推送到背后  

6.  sendSubviewToBack:  

7.  //把视移除  

8.  removeFromSuperview  

9.  //插入视 并指定索引  

10. insertSubview:atIndex:  

11. //插入视在某个视之上  

12. insertSubview:aboveSubview:  

13. //插入视在某个视之下  

14. insertSubview:belowSubview:  

15. //两个位置索引的视  

16. exchangeSubviewAtIndex:withSubviewAtIndex:  

 

二.UITextView

1.           text: 设置textView中文本

_textView.text =@"Now is the time for all good developers to come toserve their country.\n\nNow is the time for all good developers to come toserve their country.";// 设置它显示的内容

2.           font:设置textView中文字的字体

_textView.font = [UIFontfontWithName:@"Arial"size:18.0];// 设置字体名字和字体大小

 

3.           textColor:设置textView中文本的颜色

_textView.textColor = [UIColorblackColor]; // 设置textview里面的字体颜色

 

4.           textAlignment:设置textView的文本的排列方式

_textView.textAlignment =NSTextAlignmentCenter; // textView中的文本排列,默认靠左

5.           backgroundColor:设置textView的背景颜色

_textView.backgroundColor = [UIColorgrayColor]; // 设置浅灰色的背景色,默认为白色

 

6.           delegate:设置代理

_textView.delegate =self; // 设置代理

7.           editable:设置textView是否可被输入

_textView.editable =NO; // textView是否可被输入,默认为YES

 

8.           attributedText:设置默认插入textView的文字

_textView.attributedText = [[NSAttributedStringalloc]initWithString:@"attributedText__-abc"];// 可以方便将文本插入到UITextView中。

9.           inputView:设置从底部弹出的视图

_textView.inputView = [[UIDatePickeralloc]init];// 弹出视图,默认为键盘

 

10.   inputAccessoryView:设置弹出视图上方的辅助视图

_textView.inputAccessoryView = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];// 弹出视图上方的辅助视图

 

11.   clearsOnInsertion:设置textView获得焦点,在用户使用虚拟键盘进行输入时,清除之前的文本

_textView.clearsOnInsertion =YES; // clearsOnInsertion,默认为NO

 

 

三.UILabel属性

1.text:设置标签显示文本。 

2.attributedText:设置标签属性文本。 

Ios代码 

NSString *text = @"first";  

NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:text];  

[textLabelStr setAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor],   

 NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(1110)];

label.attributedText = textLabelStr;  

3.font:设置标签文本字体。

默认是系统自带字体,大小为17。

Ios代码 

label.font = [UIFontsystemFontOfSize:17]

label.font = [UIFont fontWithName:@"Arial" size:16];  

Ios代码  

label.textColor = [UIColor blueColor];

4.textAlignment:设置标签文本对齐方式。 

Ios代码  

label.textAlignment = NSTextAlignmentCenter;  

5.lineBreakMode:设置标签文字过长时的显示方式,这个属性使用于label中文本的换行和截短。首先numberofLines必须设置为0,才有效果。

 

Ios代码  

label.lineBreakMode = NSLineBreakByCharWrapping;以字符为显示单位显示,后面部分省略不显示。  

label.lineBreakMode = NSLineBreakByClipping;剪切与文本宽度相同的内容长度,后半部分被删除。  

label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字以……方式省略,显示尾部文字内容。  

label.lineBreakMode = NSLineBreakByTruncatingMiddle;中间的内容以……方式省略,显示头尾的文字内容。  

label.lineBreakMode = NSLineBreakByTruncatingTail;结尾部分的内容以……方式省略,显示头的文字内容。  

label.lineBreakMode = NSLineBreakByWordWrapping;以单词为显示单位显示,后面部分省略不显示。  

比如:

label.numberOfLines = 0

label.lineBreakMode = NSLineBreakByTruncatingMiddle;

实现效果:


6.enabled:设置文字内容是否可变。 

7.adjustsFontSizeToFitWidth:文字内容自适应标签宽度。 

8.adjustsLetterSpacingToFitWidth:根据字母的间隔自适应标签宽度,超出部分以……显示。 

9.numberOfLines:标签最多显示行数。 

10.minimumScaleFactor:设置最小字体,与minimumFontSize相同,minimumFontSize在IOS 6后不能使用。 

11.highlightedTextColor:设置文本高亮显示颜色,与highlighted一起使用。 

12.shadowColor:设置文本阴影颜色。 

13.shadowColor:设置文本阴影与原文本的偏移量。label.shadowOffset= CGSizeMake(1.0, 5.0); 
14.userInteractionEnabled:设置标签是否忽略或移除用户交互。默认为NO。 

15.preferredMaxLayoutWidth:优先选择标签布局的最大宽度。 

16.baselineAdjustment:如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为。

 

 

Ios代码 

label4.baselineAdjustment = UIBaselineAdjustmentNone;  

UIBaselineAdjustmentAlignBaselines=0,默认,文本最上端与中线对齐。  

UIBaselineAdjustmentAlignCenters,   文本中线与label中线对齐。  

UIBaselineAdjustmentNone,  文本最低端与label中线对齐。

 

17. backgroundColor背景颜色

ios代码:清空背景颜色 label1.backgroundColor= [UIColor clearColor];

 

 

四.UIButton属性

1.UIButton状态:

UIControlStateNormal          // 正常状态   
UIControlStateHighlighted     // 高亮状态   
UIControlStateDisabled        // 禁用状态    
UIControlStateSelected        // 选中状态    
UIControlStateApplication     //     
UIControlStateReserved        // 保留状态

2.Uibutton类型:

UIButtonTypeCustom            //自定义类型
添加图片: 灰色背景颜色:
UIButtonTypeRoundedRect       //圆角类型
UIButtonTypeDetailDisclosure   //细节展示按钮
UIButtonTypeInfoLight          //浅色背景的信息按钮
UIButtonTypeInfoDark           //暗色背景的信息按钮
UIButtonTypeContactAdd         // 添加按钮

3.UIButton常用属性

给按钮设置文字时,苹果文档说明,不能使用label对象设置文字的颜色或者阴影颜色,相反必须使用setTitleColor:forState: andsetTitleShadowColor:forState:这两个方法才能修改这些值。

设置按钮中其他属性依次类推。。。。

 

//设置对应状态的标题内容default is nil. title is assumed to besingle line

- (void)setTitle:(NSString*)title forState:(UIControlState)state;  

//设置对应状态的标题颜色          

- (void)setTitleColor:(UIColor*)color forState:(UIControlState)state;   

//设置对应状态的标题阴影颜色           

- (void)setTitleShadowColor:(UIColor*)color forState:(UIControlState)state;          

//设置对应状态的按钮的图片

- (void)setImage:(UIImage*)image forState:(UIControlState)state;        

//设置对应状态的按钮背景图片

- (void)setBackgroundImage:(UIImage*)image forState:(UIControlState)state;

 

添加事件

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

这些事件都是基于触摸、基于值、基于编辑。有如下事件会触发。

在点击按钮是按钮是凹下去,然后弹起才触发起事件,按钮的状态有:

1.   UIControlEventTouchDown      // 按下    

2.      

3.   UIControlEventTouchDownRepeat  多次按下   

4.   UIControlEventTouchUpInside // 在按及其一定外围内

5.      

6.   UIControlEventTouchUpOutside // 外面松  

 

4.adjustsImageWhenDisabled

当按钮禁用的情况下,图像的颜色会被画深一点,默认为YES。

5.adjustsImageWhenHighlighted

当按钮高亮的情况下,图像的颜色会被画深一点,默认为YES。

6.showsTouchWhenHighlighted

button.showsTouchWhenHighlighted=YES;点击时的闪光效果会被前景图片遮住中间部分;

6.contentEdgeInsets

设置按钮的内部内容(包含按钮图片和标题)离按钮边缘上下左右的距离。

 

7.按钮实例

1.有些时候我们想让UIButton的title居左对齐,我们设置 
btn.textLabel.textAlignment= UITextAlignmentLeft

是没有作用的,我们需要设置 

btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

但是问题又出来,此时文字会紧贴到左边框,我们可以设置 

btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

使文字距离左边框保持10个像素的距离。 

 

五.UIControl事件

1.UIControlEventTouchDown

单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。

2.UIControlEventTouchDownRepeat

多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。

3.UIControlEventTouchDragInside

当一次触摸在控件窗口内拖动时。

4.UIControlEventTouchDragOutside

当一次触摸在控件窗口之外拖动时。

5.UIControlEventTouchDragEnter

当一次触摸从控件窗口之外拖动到内部时。

6.UIControlEventTouchDragExit

当一次触摸从控件窗口内部拖动到外部时。

7.UIControlEventTouchUpInside

所有在控件之内触摸抬起事件。

8.UIControlEventTouchUpOutside

所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)

9.UIControlEventTouchCancel

所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。

10.UIControlEventTouchChanged

当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。

11.UIControlEventEditingDidBegin

当文本控件中开始编辑时发送通知。

12.UIControlEventEditingChanged

当文本控件中的文本被改变时发送通知。

13.UIControlEventEditingDidEnd

当文本控件中编辑结束时发送通知。

14.UIControlEventEditingDidOnExit

当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。

15.UIControlEventAlltouchEvents

通知所有触摸事件。

16.UIControlEventAllEditingEvents

通知所有关于文本编辑的事件。

17.UIControlEventAllEvents

通知所有事件

六.UIAlertView

1.Title

获取或设置UIAlertView上的标题。

 

2.Message

获取或设置UIAlertView上的消息

 

    UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"Title"message:@"message"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

    alertView.title = @"T";

    alertView.message = @"M";

 

    [alertView show];

3.numberOfButtons (只读)

返回UIAlertView上有多少按钮.

UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"Title"message:@"message"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

    NSLog(@"%d",alertView.numberOfButtons);

[alertView show];

 

4.cancelButtonIndex

   UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"

                                                           message:@"请选择一个按钮:"

                                                          delegate:nil

                                                cancelButtonTitle:@"取消"

                                           otherButtonTitles:@"按钮一",@"按钮二",@"按钮三",nil];

   [alert show];

NSLog(@"UIAlertView中取消按钮的角标是%d",alert.cancelButtonIndex);

效果:

注意不要认为取消按钮的角标是4“取消”,“按钮一”,“按钮二”,“按钮三”的索引buttonIndex分别是0,1,2,3

 

 

5. alertViewStyle

5.1 UIAlertViewStyleLoginAndPasswordInput

UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"产品信息展示" message:p.namedelegate:nilcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

   

alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

   

// 弹出UIAlertView

 [alert show];

 

 

5.2 UIAlertViewStylePlainTextInput

UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"产品信息展示" message:p.namedelegate:nilcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

   

alert.alertViewStyle = UIAlertViewStylePlainTextInput;

   

// 弹出UIAlertView

 [alert show];

 

5.3UIAlertViewStyleSecureTextInput

UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"产品信息展示" message:p.namedelegate:nilcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

   

alert.alertViewStyle = UIAlertViewStyleSecureTextInput;

   

// 弹出UIAlertView

 [alert show];


6. - (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex

返回textFieldIndex角标对应的文本框。

取出文本框文字

7.手动的取消对话框

[alert dismissWithClickedButtonIndex:0 animated:YES];

 

8. delegate

作为UIAlertView的代理,必须遵守UIAlertViewDelegate。

1.当点击UIAlertView上的按钮时,就会调用,并且当方法调完后,UIAlertView会自动消失。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

 

2.当UIAlertView即将出现的时候调用

- (void)willPresentAlertView:(UIAlertView *)alertView;

3. 当UIAlertView完全出现的时候调用

- (void)didPresentAlertView:(UIAlertView *)alertView; 

 

4. 当UIAlertView即将消失的时候调用

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;

5. 当UIAlertView完全消失的时候调用

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex; 

 

9.注意UIAlertView调用show显示出来的时候,系统会自动强引用它,不会被释放。

10. 为UIAlertView添加子视图

在为UIAlertView对象太添加子视图的过程中,有点是需要注意的地方,如果删除按钮,也就是取消UIAlerView视图中所有的按钮的时候,可能会导致整个显示结构失衡。按钮占用的空间不会消失,我们也可以理解为这些按钮没有真正的删除,仅仅是他不可见了而已。如果在UIAlertview对象中仅仅用来显示文本,那么,可以在消息的开头添加换行符(@"\n)有助于平衡按钮底部和顶部的空间。

下面的代码用来演示如何为UIAlertview对象添加子视图的方法。

UIAlertView*alert =[[UIAlertView alloc]initWithTitle:@"请等待" 

                                                 message:nil

                                                 delegate:nil   

                                                 cancelButtonTitle:nil 

                                                 otherButtonTitles:nil]; 

[alert show];

UIActivityIndicatorView*activeView =[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

activeView.center = CGPointMake(alert.bounds.size.width/ 2.0f, alert.bounds.size.height - 40.0f);  

[activeView startAnimating];  

[alert addSubview:activeView];  

 11. UIAlertView小例子

  UIAlertView默认情况下所有的text是居中对齐的。 那如果需要将文本向左对齐或者添加其他控件比如输入框时该怎么办呢?不用担心, iPhone SDK还是很灵活的,有很多delegate消息供调用程序使用。所要做的就是在

- (void)willPresentAlertView:(UIAlertView *)alertView

中按照自己的需要修改或添加即可,比如需要将消息文本左对齐,下面的代码即可实现:

-(void) willPresentAlertView:(UIAlertView *)alertView

{

      for( UIView * view in alertView.subviews )

      {

            if([view isKindOfClass:[UILabel class]] )

            {

                  UILabel*label = (UILabel*) view;

                  label.textAlignment=UITextAlignmentLeft;

            }

      }

}

0 0
原创粉丝点击