UILabel 是 IOS 显示文字的组件,继承与UIView
属性
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
text | @property(nullable, nonatomic,copy) NSString | 文字 | nil |
font | @property(null_resettable, nonatomic,strong) UIFont | 字体大小 | 17 |
textColor | @property(null_resettable, nonatomic,strong) UIColor | 字体颜色| blackColor | |
shadowColor | @property(nullable, nonatomic,strong) UIColor | 阴影颜色 | nil |
textAlignment | @property(nonatomic) NSTextAlignment | 字体方向 | 默认为 NSTextAlignmentNatural IOS9之前为 NSTextAlignmentNatural |
lineBreakMode | @property(nonatomic) NSLineBreakMode | 换行模式 | 默认为NSLineBreakByTruncatingTail. 可用于多行和单行。 |
attributedText | @property(nullable, nonatomic,copy) NSAttributedString * | 富文本内容,设置了此属性将不显示text属性。 | nil |
enabled | @property(nonatomic,getter=isEnabled) BOOL | 是否显示 | YES |
numberOfLines | @property(nonatomic) NSInteger | 最多可显示的行数,0表示无限行数 | 1 |
adjustsFontSizeToFitWidth | @property(nonatomic) BOOL | 是否根据字体大小调整宽度 | NO |
baselineAdjustment | @property(nonatomic) UIBaselineAdjustment | 字体基线 | UIBaselineAdjustmentAlignBaselines |
allowsDefaultTighteningForTruncation | @property(nonatomic) BOOL | 是否允许截断 | NO |
简单代码
@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.[self createLableUI];[self createSuperLabelUI];
}// LabelUI
- (void)createLableUI {UILabel *label = [[UILabel alloc] init];label.text = @"hello wrod";// 设置坐标label.frame = CGRectMake(100, 100, 200, 50);// 设置背景透明颜色 clearColorlabel.backgroundColor = [UIColor clearColor];// 设置文字颜色label.textColor = [UIColor redColor];// 设置字体大小label.font = [UIFont systemFontOfSize: 40];// 设置阴影label.shadowColor = [UIColor redColor];// 设置阴影偏移量label.shadowOffset = CGSizeMake(2, 2);// 设置文字方向label.textAlignment = NSTextAlignmentCenter;// 设置文字最多行数,默认1行label.numberOfLines = 2;// 添加视图[self.view addSubview:label];
}- (void)createSuperLabelUI {UILabel *superLabel = [[UILabel alloc] init];superLabel.frame = CGRectMake(100, 300, 200, 400);superLabel.numberOfLines = 0;NSString *str = @"Im\nSuper\nLabel";NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];superLabel.attributedText = text;[self.view addSubview:superLabel];
}
@end