实例代码(仅包含ViewController.m)
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void) createUI
{
UILabel* label = [[UILabel alloc] init];
UILabel* label1 = [[UILabel alloc] init];
label.text = @"Hello World!";
label1.text = @"Hello World!";
label.frame = CGRectMake(100, 100, 160, 40);
label1.frame = CGRectMake(200, 200, 200, 90);
label.font = [UIFont systemFontOfSize:18];
label.textColor = [UIColor brownColor];
label.backgroundColor = [UIColor blueColor];
label1.backgroundColor = [UIColor redColor];
[self.view addSubview:label];
label.shadowColor = [UIColor grayColor];
label.shadowOffset = CGSizeMake(3, 3);
label.textAlignment = NSTextAlignmentCenter;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createUI];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
心得体会
- label是标签的意思,在OC中就是指一个文字框。
- 这一段是公认常用的表示换行的方法。
- 对于无法在一行中出完的字,因为初始默认numberOfLines是一行,使得无法显示的出现省略号。单写一行numberOfLines = 0会使得它自动换行(但依然走不出划定区域。应该说划定区域还是优先级最高)。而第一行对于字符过长处理参看第二段代码,总之这两行时常用的换行处理。
- 另外要注意的事尽管设置了自动换行,但手动在文字中加入\n也是会换行的
lineBreakMode
label.lineBreakMode = NSLineBreakByCharWrapping;
示,后面部分省略不显示。
label.lineBreakMode = NSLineBreakByClipping;
label.lineBreakMode = NSLineBreakByTruncatingHead;
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
label.lineBreakMode = NSLineBreakByTruncatingTail;
label.lineBreakMode = NSLineBreakByWordWrapping;
作者:黑暗森林的歌者
链接:https:
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。