OC常用入门代码.docx

上传人:11****ws 文档编号:3263148 上传时间:2019-05-27 格式:DOCX 页数:29 大小:372.46KB
下载 相关 举报
OC常用入门代码.docx_第1页
第1页 / 共29页
OC常用入门代码.docx_第2页
第2页 / 共29页
OC常用入门代码.docx_第3页
第3页 / 共29页
OC常用入门代码.docx_第4页
第4页 / 共29页
OC常用入门代码.docx_第5页
第5页 / 共29页
点击查看更多>>
资源描述

1、常用代码整理:Xcode6,引入 viewController 代码:1. 在 Appdelegate 先引入 ViewController 头文件2. ViewController vc =(ViewController alloc)init;Self.window.rootViewController = vc;然后 vc 就代表 ViewController 对象,就可以调用其属性和方法了。一般用通知来取代上面的写法:/发 送通知NSNotificationCenter defaultCenter postNotificationName:通知名object:nil;/接受通知/IB 创

2、建的对象调用 initWithCoder 要复写方法;-(instancetype) initWithCoder(NSCoder*)coderself = super initWithCoder:coder;if(self) NSNotificationCenter defaultCenteraddObserver:self selector:selector(method)name:”.”object:nil ;return self;UILabel 多行文字自动换行 (自动折行)UIView *footerView = UIView alloc initWithFrame:CGRectMa

3、ke(10, 100, 300, 180); UILabel *label = UILabel alloc initWithFrame:CGRectMake(10, 100, 300, 150); label.text = “where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you?“; /清空背景颜色 label.backgroundColor = UIC

4、olor clearColor; /设置字体颜色为白色 label.textColor = UIColor whiteColor; /文字居中显示 label.textAlignment = UITextAlignmentCenter; /自动折行设置 label.lineBreakMode = UILineBreakModeWordWrap; label.numberOfLines = 0;iOS 的 UILabel 设置居上对齐,居中对齐,居下对齐在 iOS 中默认的 UILabel 中的文字在竖直方向上只能居中对齐,博主参考国外网站,从 UILabel 继承了一个新类,实现了居上对齐,居

5、中对齐,居下对齐。具体如下:typedef enum VerticalAlignmentTop = 0, / default VerticalAlignmentMiddle, VerticalAlignmentBottom, VerticalAlignment; interface myUILabel : UILabel private VerticalAlignment _verticalAlignment; property (nonatomic) VerticalAlignment verticalAlignment; end implementation myUILabel synth

6、esize verticalAlignment = verticalAlignment_; - (id)initWithFrame:(CGRect)frame if (self = super initWithFrame:frame) self.verticalAlignment = VerticalAlignmentMiddle; return self; - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment verticalAlignment_ = verticalAlignment; self setNeeds

7、Display; -(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines 1. CGRect textRect = super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines; 2. switch (self.verticalAlignment) case VerticalAlignmentTop: textRect.origin.y = bounds.origin.y; break; case

8、VerticalAlignmentBottom: textRect.origin.y = bounds.origin.y + bounds.size.height -textRect.size.height; break; case VerticalAlignmentMiddle: / Fall through. default: textRect.origin.y = bounds.origin.y + (bounds.size.height -textRect.size.height) / 2.0; return textRect; -(void)drawTextInRect:(CGRec

9、t)requestedRect CGRect actualRect = self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines; super drawTextInRect:actualRect; end lbl_mylabel = myUILabel alloc initWithFrame:CGRectMake(20, 50, 150, 600); UIColor *color = UIColor colorWithPatternImage:UIImage imageNamed:“halfTr

10、ansparent.png“;/使用半透明图片作为label的背景色 lbl_mylabel.backgroundColor = color; lbl_mylabel.textAlignment = UITextAlignmentLeft; lbl_mylabel.textColor = UIColor.whiteColor; lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap; lbl_mylabel.numberOfLines = 0; lbl_mylabel setVerticalAlignment:VerticalAlignmentT

11、op; self addSubview:lbl_mylabel; ios UILabel 变量名不能为 title-UILabel copyWithZone: unrecognized selector sent to instance遇到了这样一个错误,找了半天没找到是什么错误,于是,Google搜索,打开第一个链接http:/ 设置过长文本中间为省略号label.lineBreakMode = NSLineBreakByTruncatingMiddle;参考: iOS组件之UILabeliOS UILabel详解http:/ rect = CGRectMake(100, 200, 50,

12、50);UILabel *label = UILabel alloc initWithFrame:rect;2、text /设置和读取文本内容,默认为nillabel.text = ”文本信息”; /设置内容NSLog(”%”, label.text); /读取内容3、textColor /设置文字颜色,默认为黑色lable.textColor = UIColor redColor;4、font /设置字体大小,默认17label.font = UIFont systemFontOfSize:20; /一般方法label.font = UIFont boldSystemFontOfSize:2

13、0; /加粗方法label.font = UIFont fontWithName:“Arial“ size:16; /指定字体的方法/还有 一种从外部导入字体的方法。5、textAlignment /设置标签文本对齐方式。label.textAlignment = NSTextAlignmentCenter; /还有NSTextAlignmentLeft、 NSTextAlignmentRight.6、numberOfLines /标签最多显示行数,如果为0则表示多行。label.numberOfLines = 2;7、enabled /只是决定了Label的绘制方式,将它设置为NO将会使文本

14、变暗,表示它没有激活,这时向它设置颜色值是无效的。label.enable = NO;8、highlighted /是否高亮显示label.highlighted = YES;label.highlightedTextColor = UIColor orangeColor; /高亮显示时的文本颜色9、ShadowColor /设置阴影颜色 label setShadowColor:UIColor blackColor;10、ShadowOffset /设置阴影偏移量label setShadowOffset:CGSizeMake(-1, -1);11、baselineAdjustment /如

15、果adjustsFontSizeToFitWidth 属性设置为YES ,这个属性就来控制文本基线的行为。label.baselineAdjustment = UIBaselineAdjustmentNone;UIBaselineAdjustmentAlignBaselines = 0,默认,文本最上端与中线对齐。UIBaselineAdjustmentAlignCenters, 文本中线与label 中线对齐。UIBaselineAdjustmentNone, 文本最低端与label中线对齐。12、Autoshrink /是否自动收缩Fixed Font Size 默认,如果Label宽度小

16、于文字长度时时, 文字大小不自动缩放minimumScaleFactor 设置最小收缩比例,如果Label宽度小于文字长度时,文字进行收缩,收缩超过比例后,停止收缩。minimumFontSize 设置最小收缩字号,如果Label宽度小于文字长度时,文字字号减小,低于设定字号后,不再减小。/6.0以后不再使用了。label.minimumScaleFactor = 0.5;13、adjustsLetterSpacingToFitWidth /改变字母之间的间距来适应 Label大小myLabel.adjustsLetterSpacingToFitWidth = NO;14、 lineBreak

17、Mode /设置文字过长时的显示格式 label.lineBreakMode = NSLineBreakByCharWrapping;以字符为显示单位显示,后面部分省略不显示。label.lineBreakMode = NSLineBreakByClipping;剪切与文本宽度相同的内容长度,后半部分被删除。label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字以方式省略,显示尾部文字内容。label.lineBreakMode = NSLineBreakByTruncatingMiddle;中间的内容以方式省略,显示头尾的文字内容。l

18、abel.lineBreakMode = NSLineBreakByTruncatingTail;结尾部分的内容以方式省略,显示头的文字内容。label.lineBreakMode = NSLineBreakByWordWrapping;以单词为显示单位显示,后面部分省略不显示。15、 adjustsFontSizeToFitWidth /设置字体大小适应label宽度 label.adjustsFontSizeToFitWidth = YES;16、attributedText:设置标签属性文本。NSString *text = “first“;NSMutableAttributedStri

19、ng *textLabelStr =NSMutableAttributedString allocinitWithString:text;textLabelStrsetAttributes:NSForegroundColorAttributeName :UIColor lightGrayColor, NSFontAttributeName :UIFont systemFontOfSize:17 range:NSMakeRange(11,10);label.attributedText = textLabelStr;17、竖排文字显示每个文字加一个换行符,这是最方便和简单的实现方式。label.

20、text = “请n竖n 直 n方n 向n排n列“ ;label.numberOfLines = label.text length;18、计算UIlabel 随字体多行后的高度CGRect bounds = CGRectMake(0, 0, 200, 300);heightLabel = myLabel textRectForBounds:boundslimitedToNumberOfLines:20; /计算20 行后的Label 的FrameNSLog(“%f“,heightLabel.size.height);19、UILabel根据字数多少自动实现适应高度UILabel *msgLa

21、bel = UILabel allocinitWithFrame:CGRectMake(15, 45, 0, 0);msgLabel.backgroundColor = UIColor lightTextColor;msgLabel setNumberOfLines:0;msgLabel.lineBreakMode = UILineBreakModeWordWrap;msgLabel.font = UIFont fontWithName:“Arial“ size:12;CGSize size = CGSizeMake(290, 1000);msgLabel.text = “获取到的device

22、Token ,我们可以通过webservice服务提交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net 应用环境中使用。“;CGSize msgSie = msgLabel.text sizeWithFont:fontsconstrainedToSize:size;msgLabel setFrame:CGRectMake(15, 45, 290, msgSie.height);20、渐变字体LabelUIColor *titleColor = UIColor colorWithPatternImage:UIImageimageNamed:“btn.png“;NSString *

23、title = “Setting“;UILabel *titleLabel = UILabel allocinitWithFrame:CGRectMake(0, 0, 80, 44);titleLabel.textColor = titleColor;titleLabel.text = title;titleLabel.font = UIFont boldSystemFontOfSize:20;titleLabel.backgroundColor = UIColor clearColor;self.view addSubview:titleLabel;titleLabel release;21

24、、Label 添加边框titleLabel.layer.borderColor = UIColor grayColor CGColor;titleLabel.layer.borderWidth = 2;UIButton *btn = UIButton buttonWithType:UIButtonTypeRoundedRect;/初始化button,选择 button类型btn.frame = CGRectMake(30, 360, 90, 35);/大小和位置btn setTitle:“ZoomIn“ forState:UIControlStateNormal;/正常状况下button 显示

25、的标题btn setTitle:“ZoomIn“ forState:UIControlStateHighlighted;/高亮显示时button的标题btn addTarget:self action:selector(zoomInAction:) forControlEvents:UIControlEventTouchUpInside;/button被按下又抬起后发生的事件/selector可以理解为“选择子“,selector是一个指针变量,类似于sender。 这里是将method的方法指定给新建的这个btn。/*在 method 方法里可以将 sender 看作是 btn 了 比如设置

26、btn的hidden属性等等 btn.hidden = YES; 这样 btn被隐藏了/*/ 通过背景图片设置按钮高亮IImage *normalImage = UIImage imageNamed:“NormalBlueButton.png“; UIImage *highlightedImage = UIImage imageNamed:“HighlightedBlueButton“;self.myButton = UIButton buttonWithType:UIButtonTypeCustom;self.myButton.frame = CGRectMake(110.0f,200.0f

27、,100.0f, 37.0f);self.myButton setBackgroundImage:normalImage forState:UIControlStateNormal;self.myButton setBackgroundImage:highlightedImage forState:UIControlStateHighlighted;self.myButton setTitle:“Normal“ forState:UIControlStateNormal;self.myButton setTitle:“Pressed“ forState:UIControlStateHighli

28、ghted;1.手写UIButtonCGRect btnControlRect=CGRectMake(200, theHeight, 100, 30);UIButton *btnControl=UIButton alloc initWithFrame:btnControlRect;/ 设置按钮的背景图片btnControl setBackgroundImage:UIImage imageNamed:“desc_nomal.png“ forState:UIControlStateNormal;btnControl setBackgroundImage:UIImage imageNamed:“de

29、sc_nomal.png“ forState:UIControlStateHighlighted;btnControl setBackgroundImage:UIImage imageNamed:“desc_nomal.png“ forState:UIControlStateSelected;btnControl setBackgroundColor:UIColor clearColor;btnControl addTarget:self action:selector(ClickControlAction:) forControlEvents:UIControlEventTouchUpIns

30、ide;self.scrollView addSubview:btnControl;btnControl release;/ 设置按钮的文本_btnOrder setTitle:“预定“ forState:UIControlStateNormal;_btnOrder setTitle:“取消“ forState:UIControlStateSelected;/ 设置按钮的文本颜色_btnOrder setTitleColor:UIColor colorWithRed:50/255.0 green:50/255.0 blue:50/255.0alpha:1.0 forState:UIContro

31、lStateNormal;_btnOrder setTitleColor:UIColor colorWithRed:255/255.0 green:255/255.0blue:255/255.0 alpha:1.0 forState:UIControlStateSelected;/ 设置按钮的文本大小_btnOrder.titleLabel setFont:UIFont systemFontOfSize:14;UITextField 的总结1.UITextField 的初始化和设置textField = UITextField alloc initWithFrame:CGRectMake(12

32、0.0f, 80.0f, 150.0f, 30.0f); textField setBorderStyle:UITextBorderStyleRoundedRect; /外框类型 textField.placeholder = “password“; /默认显示的字 textField.secureTextEntry = YES; /密码 textField.autocorrectionType = UITextAutocorrectionTypeNo; textField.autocapitalizationType = UITextAutocapitalizationTypeNone; textField.returnKeyType = UIReturnKeyDone; textField.clearButtonMode = UITextFieldViewModeWhileEditing; /编辑时会出现个修改 X textField.delegate = self;2.要实现的 Delegate 方法,关闭键盘

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 重点行业资料库 > 医药卫生

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。