UILabel的使用
一、初始化
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)]; [self.view addSubview:myLabel];
①、设置默认文本
NSString *text = @"标签文本"; myLabel.text = text;
②、设置标签文本(此属性是iOS6.0之后才出现,如若不是必要,不建议使用此属性)
NSString *text = @"其实没什么"; NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text]; [attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(2, 1)]; myLabel.attributedText = attributeString;
关键字标红的效果
NSString *keyword = @"脚本"; NSString *result = @"呐喊教程"; // 设置标签文字 NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:result]; // 获取标红的位置和长度 NSRange range = [result rangeOfString:keyword]; // 设置标签文字的属性 [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:range]; // 显示在Label上 label.attributedText = attrituteString;
myLabel.font = [UIFont systemFontOfSize:13];
myLabel.textColor = [UIColor blueColor];
myLabel.textAlignment = NSTextAlignmentCenter;//居中NSTextAlignmentLeft //左对齐 NSTextAlignmentCenter //居中 NSTextAlignmentRight //右对齐 NSTextAlignmentJustified//最后一行自然对齐 NSTextAlignmentNatural //默认对齐脚本
⑥、文字剪裁方式
NSLineBreakByWordWrapping = 0,//以空格为边界,保留单词 NSLineBreakByCharWrapping, //保留整个字符 NSLineBreakByClipping, //简单剪裁,到边界为止 NSLineBreakByTruncatingHead, //按照"……文字"显示 NSLineBreakByTruncatingTail, //按照"文字……文字"显示 NSLineBreakByTruncatingMiddle //按照"文字……"显示myLabel.lineBreakMode = NSLineBreakByTruncatingHead;
myLabel.enabled = NO;
myLabel.adjustsFontSizeToFitWidth = YES;
②、改变字母之间的间距来适应label大小
当这个属性是YES,标签可能改变标签文本的字母间距,以使该文本更适合标签的边界内。此属性的字符串,而不管当前行的行的裁剪模式。该属性的默认值是NO。
myLabel.adjustsLetterSpacingToFitWidth = NO;
myLabel.adjustsFontSizeToFitWidth = YES;//调整基线位置需将此属性设置为YESmyLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
UIBaselineAdjustmentAlignBaselines //文本最上端与Label中线对齐,默认值 UIBaselineAdjustmentAlignCenters //文本中线与Label中线对齐 UIBaselineAdjustmentNone //文本最下端与Label中线对齐
iOS6.0之后:minimumScaleFactor
myLabel.minimumScaleFactor = 10.0;//默认值为0,为当前字体大小
⑤、行数
myLabel.numberOfLines = 2;//Label行数
myLabel.highlighted = YES;//是否高亮 myLabel.highlightedTextColor = [UIColor redColor];//高亮颜色;此属性在设置按钮的titleLabel时,无论highlighted是YES还是NO,在按钮按下时标题都显示此高亮颜色
myLabel.shadowColor = [UIColor grayColor];//阴影颜色,默认为nil myLabel.shadowOffset = CGSizeMake(1, 1);//阴影的偏移点
CGRect result,bounds; bounds = CGRectMake(0, 0,200, 300); heightLabel = [myLabel textRectForBounds:bounds limitedToNumberOfLines:20];//计算20行后的Label的Frame NSLog(@"%f",heightLabel.size.height);
- (void)drawTextInRect:(CGRect)rect //需要重载此方法,然后由子类调用,重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了
UIButton的简单使用
Button(按钮)是使用最频繁的控件之一,UIButton的使用并不难,但是有一点必须要注意
UIButton默认Type是Rounded Rect button,这个形态是不被App Store所接受的,即便是你给这个button添加了背景图片,不再是Rounded Rect 的外观,还是会被App Store所拒,总之Button的类型不能是Rounded Rect。
一、UIButton的外观
1.1 UIButton有6种类型, 如下图所示
UIButtonTypeCustom UIButtonTypeRoundedRect UIButtonTypeDetailDisclosure UIButtonTypeInfoLight UIButtonTypeInfoDark UIButtonTypeContactAdd
1.2 设置buttonType
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 44)]; [UIButton buttonWithType:UIButtonTypeCustom];
2.1可以设置正常状态,不可点击状态,选中状态等各种形态下的背景图片
// 正常状态下的图片 [button setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal]; // 点击后的图片 [button setBackgroundImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateSelected];
UIImage *buttonNormal = [UIImage imageNamed:@"button-white-part"]; // 进行宽度上的延伸,如果要进行高度上的延伸,改变第一个和第三个参数的值,此方法是保证图片边缘的框架的前提下对图片中间进行高度或者宽度的延伸 UIImage *stretchNormal = [buttonNormal resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 30)]; [button setBackgroundImage:stretchNormal forState:UIControlStateNormal];
按钮事件还有好多种,可以自行查看文档
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside]; // 点击事件 - (IBAction)buttonPress:(id)sender { NSLog("@"button pressed!"); }
4.1 在View中拖动一个Button放入画布
4.2 设置button属性
4.3 创建映射,以及按钮事件
选中xib,点击xcode右上方的Editor
,让一个界面为xib,另一个界面为其对应的.h文件。
选中button 右键拖拽出一条线到.h文件@interface与@end之间的位置,如果要创建映射则选择IBOutlet,创建按钮事件则选择Action,填写完成后点击connet即可
在.m文件中会出现所创建的点击事件,在方法里完成对应的操作即可。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。