UILabel 总结

来源:互联网 发布:c语言自己写头文件 编辑:程序博客网 时间:2024/05/15 02:47
#import "MainViewController.h"#define WIDTH self.view.frame.size.width#define HEIGHT self.view.frame.size.height@interface MainViewController ()@property(nonatomic,retain)UILabel *label;@end@implementation MainViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    // 创建一个label    self.label=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, WIDTH, 100)];    self.label.layer.borderWidth=1;    self.label.layer.cornerRadius=10;    self.label.layer.masksToBounds=YES;    [self.view addSubview:self.label];    [self.label release];    // label的文本内容    self.label.text=@"jfkld sfjdklsjfkldsjaklfdjsk lajfdklsajfkldsjaklfdj kslajfdklsajfoeiroeu wioafndnfdaifoejwi omdnafkldjsaoieowjkoewnfnksnflkdnsafdjisafklewklfnewofuiodas";    // 文本颜色    self.label.textColor=[UIColor redColor];    // 文本的对齐方式    // 对齐方式为枚举,用数字表示也行    self.label.textAlignment=NSTextAlignmentLeft;    // 字体大小    self.label.font=[UIFont systemFontOfSize:20];    [UIFont systemFontSize];    // 设置行数  系统默认是一行    self.label.numberOfLines=3;    /*     这种设置行数的方式设置后,如果文本的长度超过了三行之后,之后的文本内容会显示为...     */    // 阴影颜色    self.label.shadowColor=[UIColor blackColor];    // 阴影偏移量    self.label.shadowOffset=CGSizeMake(10, 10);    // 断行方式    self.label.lineBreakMode=NSLineBreakByWordWrapping;    /*     当一行文本中最后一个单词的长度超过了行剩余的长度的话,就会把这个单词在下一行显示,也是系统默认的断行方式     */    self.label.lineBreakMode=NSLineBreakByCharWrapping;    /*     根据字符来断行,就算最后一个单词已经放不下也会断行,而单词会被拆开在两行     */    self.label.lineBreakMode=NSLineBreakByClipping;    /*     clip:修剪  在wordWrapping的基础上将超出label范围的文本剪切掉,不会显示...     */  //  NSLineBreakByTruncatingHead,  /* Truncate at head of line: "...wxyz" */  //  NSLineBreakByTruncatingTail,  /* Truncate at tail of line: "abcd..." */  //  NSLineBreakByTruncatingMiddle /* Truncate middle of line:  "ab...yz" */    self.label.highlighted=YES;    // 打开用户交互    self.label.userInteractionEnabled=YES;    /*     一个BOOL值,表示该节点是否接受触摸事件,默认为NO;     */    // 文本适应label的宽度,可以把所有label的文本放进去,自适应文本字体大小    self.label.adjustsFontSizeToFitWidth=YES;    self.label.adjustsLetterSpacingToFitWidth=YES;}- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines{    self.label.numberOfLines=3;    return CGRectMake(0, 200, WIDTH, 50);}
0 0