赞
踩
UILabel 多行文字自动换行 (自动折行)
1.UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 300, 180)]; 2. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 150)]; 3. 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?"; 4. //清空背景颜色 5. label.backgroundColor = [UIColor clearColor]; 6. //设置字体颜色为白色 7. label.textColor = [UIColor whiteColor]; 8. //文字居中显示 9. label.textAlignment = UITextAlignmentCenter; 10. //自动折行设置 11. label.lineBreakMode = UILineBreakModeWordWrap; 12. label.numberOfLines = 0;
在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐。具体如下:
在使用时:
-[UILabel copyWithZone:]: unrecognized selector sent to instance
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
·UILable是iPhone界面最基本的控件,主要用来显示文本信息。 ·常用属性和方法有: 1、创建 CGRect rect = CGRectMake(100, 200, 50, 50); UILabel *label = [[UILabel alloc] initWithFrame:rect]; 2、text //设置和读取文本内容,默认为nil label.text = @”文本信息”; //设置内容 NSLog(@”%@”, label.text); //读取内容 3、textColor //设置文字颜色,默认为黑色 lable.textColor = [UIColor redColor]; 4、font //设置字体大小,默认17 label.font = [UIFont systemFontOfSize:20]; //⼀一般方法 label.font = [UIFont boldSystemFontOfSize:20]; //加粗方法 label.font = [UIFont fontWithName:@"Arial" size:16]; //指定 字体的方法 //还有⼀一种从外部导入字体的方法。 5、textAlignment //设置标签文本对齐方式。 label.textAlignment = NSTextAlignmentCenter; //还有 NSTextAlignmentLeft、 NSTextAlignmentRight. 6、numberOfLines //标签最多显示行数,如果为0则表示多行。 label.numberOfLines = 2; 7、enabled //只是决定了Label的绘制方式,将它设置 为NO将会使文本变暗,表示它没有激活,这时向它设置颜色值是无效的。 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 //如果adjustsFontSizeToFitWidth属性设 置为YES,这个属性就来控制文本基线的行为。 label.baselineAdjustment = UIBaselineAdjustmentNone; UIBaselineAdjustmentAlignBaselines = 0,默认,文本最上端与中线对齐。 UIBaselineAdjustmentAlignCenters, 文本中线与label中线对齐。 UIBaselineAdjustmentNone, 文本最低端与label中线对齐。 12、Autoshrink //是否自动收缩 Fixed Font Size 默认,如果Label宽度小于文字长度时时,文字大小不自动缩放 minimumScaleFactor 设置最小收缩比例,如果Label宽度小于文字长度时,文字 进行收缩,收缩超过比例后,停止收缩。 minimumFontSize 设置最小收缩字号,如果Label宽度小于文字长度时,文字字号 减小,低于设定字号后,不再减小。//6.0以后不再使用了。 label.minimumScaleFactor = 0.5; 13、adjustsLetterSpacingToFitWidth //改变字母之间的间距来适应Label大小 myLabel.adjustsLetterSpacingToFitWidth = NO; 14、 lineBreakMode //设置文字过长时的显示格式 label.lineBreakMode = NSLineBreakByCharWrapping;以字符为显示单位显 示,后面部分省略不显示。 label.lineBreakMode = NSLineBreakByClipping;剪切与文本宽度相同的内 容长度,后半部分被删除。 label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字 以……方式省略,显示尾部文字内容。 label.lineBreakMode = NSLineBreakByTruncatingMiddle;中间的内容 以……方式省略,显示头尾的文字内容。 label.lineBreakMode = NSLineBreakByTruncatingTail;结尾部分的内容 以……方式省略,显示头的文字内容。 label.lineBreakMode = NSLineBreakByWordWrapping;以单词为显示单位显 示,后面部分省略不显示。 15、 adjustsFontSizeToFitWidth //设置字体大小适应label宽度 label.adjustsFontSizeToFitWidth = YES; 16、attributedText:设置标签属性文本。 NSString *text = @"first"; NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:text]; [textLabelStr setAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(11, 10)]; label.attributedText = textLabelStr; 17、竖排文字显示每个文字加一个换行符,这是最方便和简单的实现方式。 label.text = @"请\n竖\n直\n方\n向\n排\n列"; label.numberOfLines = [label.text length]; 18、计算UIlabel 随字体多行后的高度 CGRect bounds = CGRectMake(0, 0, 200, 300); heightLabel = [myLabel textRectForBounds:bounds limitedToNumberOfLines:20]; //计算20行后的Label的Frame NSLog(@"%f",heightLabel.size.height); 19、UILabel根据字数多少自动实现适应高度 UILabel *msgLabel = [[UILabel alloc] initWithFrame: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 = @"获取到的deviceToken,我们可以通过webservice服务提 交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net应用环境中使 用。"; CGSize msgSie = [msgLabel.text sizeWithFont:fonts constrainedToSize:size]; [msgLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)]; 20、渐变字体Label UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"btn.png"]]; NSString *title = @"Setting"; UILabel *titleLabel = [[UILabel alloc] initWithFrame: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、Label添加边框 titleLabel.layer.borderColor = [[UIColor grayColor] CGColor]; titleLabel.layer.borderWidth = 2;
#import "LabelTestViewController.h"
@implementation LabelTestViewController
/*
Accessing the Text Attributes
text property
font property
textColor property
textAlignment property
lineBreakMode property
enabled property
Sizing the Label’s Text
adjustsFontSizeToFitWidth property
baselineAdjustment property
minimumFontSize property 无例
numberOfLines property
Managing Highlight Values
highlightedTextColor property
highlighted property
Drawing a Shadow
shadowColor property
shadowOffset property
Drawing and Positioning Overrides
– textRectForBounds:limitedToNumberOfLines: 无例
– drawTextInRect: 无例
Setting and Getting Attributes
userInteractionEnabled property
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];
UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];
UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];
UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];
UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];
//设置显示文字
label1.text = @"label1";
label2.text = @"label2";
label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--";
label4.text = @"label4--label4--label4--label4--";
label5.text = @"label5--label5--label5--label5--label5--label5--";
label6.text = @"label6";
label7.text = @"label7";
//设置字体:粗体,正常的是 SystemFontOfSize
label1.font = [UIFont boldSystemFontOfSize:20];
//设置文字颜色
label1.textColor = [UIColor orangeColor];
label2.textColor = [UIColor purpleColor];
//设置文字位置
label1.textAlignment = UITextAlignmentRight;
label2.textAlignment = UITextAlignmentCenter;
//设置字体大小适应label宽度
label4.adjustsFontSizeToFitWidth = YES;
//设置label的行数
UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色
//设置高亮
label6.highlighted = YES;
label6.highlightedTextColor = [UIColor orangeColor];
//设置阴影
label7.shadowColor = [UIColor redColor];
label7.shadowOffset = CGSizeMake(1.0,1.0);
//设置是否能与用户进行交互
label7.userInteractionEnabled = YES;
//设置label中的文字是否可变,默认值是YES
label3.enabled = NO;
//设置文字过长时的显示格式
label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间
// typedef enum {
// UILineBreakModeWordWrap = 0,
// UILineBreakModeCharacterWrap,
// UILineBreakModeClip,//截去多余部分
// UILineBreakModeHeadTruncation,//截去头部
// UILineBreakModeTailTruncation,//截去尾部
// UILineBreakModeMiddleTruncation,//截去中间
// } UILineBreakMode;
//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为
label4.baselineAdjustment = UIBaselineAdjustmentNone;
// typedef enum {
// UIBaselineAdjustmentAlignBaselines,
// UIBaselineAdjustmentAlignCenters,
// UIBaselineAdjustmentNone,
// } UIBaselineAdjustment;
[self.view addSubview:label1];
[self.view addSubview:label2];
[self.view addSubview:label3];
[self.view addSubview:label4];
[self.view addSubview:label5];
[self.view addSubview:label6];
[self.view addSubview:label7];
[label1 release];
[label2 release];
[label3 release];
[label4 release];
[label5 release];
[label6 release];
[label7 release];
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。