初始化一个UILabel对象,并初始化大小
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
设置显示的文字
label.text=@"123";
和字体相关的一个类,字号大小默认17
@property(nonatomic,retain) UIFont*font;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
label.font=[UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
+ (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize;
+ (NSArray *)familyNames;
+ (NSArray *)fontNamesForFamilyName:(NSString *)familyName;
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;
@property(nonatomic,readonly,retain) NSString *familyName;
@property(nonatomic,readonly,retain) NSString *fontName;
@property(nonatomic,readonly) CGFloat pointSize;
@property(nonatomic,readonly) CGFloat ascender;
@property(nonatomic,readonly) CGFloat descender;
@property(nonatomic,readonly) CGFloat capHeight;
@property(nonatomic,readonly) CGFloat xHeight;
@property(nonatomic,readonly) CGFloat lineHeight NS_AVAILABLE_IOS(4_0);
@property(nonatomic,readonly) CGFloat leading;
- (UIFont *)fontWithSize:(CGFloat)fontSize;
+ (UIFont *)fontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)pointSize NS_AVAILABLE_IOS(7_0);
- (UIFontDescriptor *)fontDescriptor NS_AVAILABLE_IOS(7_0);
设置字体颜色
label.textColor=[UIColor redColor];
设置阴影偏移量
label.shadowOffset=CGSizeMake(20, 20);
设置阴影颜色
label.shadowColor=[UIColor blackColor];
设置对齐模式
label.textAlignment=NSTextAlignmentJustified;
?
1
2
3
4
5
6
7
8
9
10
11
enum
{
NSTextAlignmentLeft = 0,
NSTextAlignmentCenter = 1,
NSTextAlignmentRight = 2,
NSTextAlignmentJustified = 3,
NSTextAlignmentNatural = 4,};
typedef
NSInteger NSTextAlignment;
多行文本设置
label.lineBreakMode=NSLineBreakByCharWrapping;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
enum
{
NSLineBreakByWordWrapping = 0,
NSLineBreakByCharWrapping,
NSLineBreakByClipping,
NSLineBreakByTruncatingHead,
NSLineBreakByTruncatingTail,
NSLineBreakByTruncatingMiddle };
typedef
NSUInteger NSLineBreakMode
使用attributedText绘制
@property(nonatomic,copy) NSAttributedString *attributedText
设置高亮的字体颜色
label.highlightedTextColor=[UIColor blueColor];
//设置是否高亮
label.highlighted=YES;
用户交互 默认关闭
label.userInteractionEnabled=NO;
是否有效,默认是YES,无效为灰色
label.enabled=NO;
显示的行数,0为无限
@property(nonatomic) NSInteger numberOfLines;
宽度自适应大小 默认是NO
@property(nonatomic) BOOL adjustsFontSizeToFitWidth;
字符适应宽度:不赞成使用
@property(nonatomic) BOOL adjustsLetterSpacingToFitWidth
最小适应大小2.0-6.0
@property(nonatomic) CGFloat minimumFontSize
最小适应大小 6.0 之后
@property(nonatomic) CGFloat minimumScaleFactor
垂直方向的调整
@property(nonatomic) UIBaselineAdjustment baselineAdjustment;
?
1
2
3
4
5
6
7
typedef
enum
{
UIBaselineAdjustmentAlignBaselines,
UIBaselineAdjustmentAlignCenters,
UIBaselineAdjustmentNone,} UIBaselineAdjustment;
返回文本绘制矩形
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
文本绘制函数
- (void)drawTextInRect:(CGRect)rect
文本自动布局参数
@property(nonatomic) CGFloat preferredMaxLayoutWidth