初始化一個UILabel對象,并初始化大小
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
設定顯示的文字
label.text=@"123";
和字型相關的一個類,字号大小預設17
@property(nonatomic,retain) UIFont*font;
//7.0之後可用 設定字型風格
// NSString *const UIFontTextStyleHeadline; 用于标題的風格
// NSString *const UIFontTextStyleSubheadline;用于副标題的風格
// NSString *const UIFontTextStyleBody;用于正文的字型
// NSString *const UIFontTextStyleFootnote;用于腳注的字型
// NSString *const UIFontTextStyleCaption1;用于标準字幕字型
// NSString *const UIFontTextStyleCaption2;用于替換字幕字型
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;
//字型模型的xHeight資訊
@property(nonatomic,readonly) CGFloat xHeight;
//字型行高
@property(nonatomic,readonly) CGFloat lineHeight NS_AVAILABLE_IOS(4_0);
//模型主體資訊
@property(nonatomic,readonly) CGFloat leading;
//建立一個新字型與目前字型相同,除了指定的大小
- (UIFont *)fontWithSize:(CGFloat)fontSize;
//通過描述資訊傳回字型 7.0後可用
- (UIFont )fontWithDescriptor:(UIFontDescriptor )descriptor size:(CGFloat)pointSize NS_AVAILABLE_IOS(7_0);
//傳回字型的描述資訊,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;
enum {
//沿左邊沿對齊文本
NSTextAlignmentLeft = 0,
//中心對齊
NSTextAlignmentCenter = 1,
//右邊沿對齊
NSTextAlignmentRight = 2,
//最後一行自然對齊
NSTextAlignmentJustified = 3,
//預設對齊
NSTextAlignmentNatural = 4,};typedef NSInteger NSTextAlignment;
多行文本設定
label.lineBreakMode=NSLineBreakByCharWrapping;
//文本邊緣處理
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;
typedef enum {
//調整文本對應基線位置
UIBaselineAdjustmentAlignBaselines,
//調整文本相對其邊框的中心
UIBaselineAdjustmentAlignCenters,
//調整文本相對于邊界的左上角 預設的
UIBaselineAdjustmentNone,} UIBaselineAdjustment;
傳回文本繪制矩形
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
文本繪制函數
- (void)drawTextInRect:(CGRect)rect
文本自動布局參數
@property(nonatomic) CGFloat preferredMaxLayoutWidth