- (void)viewDidLoad { [super viewDidLoad]; // @property(nullabel, nonatomic,copy) NSString *text; // label上要顯示的文字
@property(null_resettable, nonatomic,strong) UIFont *font; // default is nil (system font 17 plain) // @property(null_resettable, nonatomic,strong) UIColor *textColor; // default is nil (text draws black) // @property(nullabel, nonatomic,strong) UIColor *shadowColor; // default is nil (no shadow)陰影顏色 // @property(nonatomic) CGSize shadowOffset; // default is CGSizeMake(0, -1) -- a top shadow // @property(nonatomic) NSTextAlignment textAlignment; // default is NSTextAlignmentLeft // @property(nonatomic) NSLineBreakMode lineBreakMode; // default is NSLineBreakByTruncatingTail. used for single and multiple lines of text // 控制文字超出label範圍後,怎樣顯示。 // // NSParagraphStyle // typedef NS_ENUM(NSInteger, NSLineBreakMode) { // NSLineBreakByWordWrapping = 0, // Wrap at word boundaries, default // NSLineBreakByCharWrapping, //自動折行,一個單詞可能現實在這行末尾,和下一行開始 // Wrap at character boundaries // NSLineBreakByClipping, //自動折行,一個單詞始終顯示在一行 // Simply clip(簡單的剪輯) // NSLineBreakByTruncatingHead, // Truncate at head of line: "...wxyz" // NSLineBreakByTruncatingTail, // Truncate at tail of line: "abcd..." // NSLineBreakByTruncatingMiddle // Truncate middle of line: "ab...yz" // } NS_ENUM_AVAIlabel(10_0, 6_0); // label.lineBreakMode = NSLineBreakByTruncatingTail; // // // the underlying attributed string drawn by the label, if set, the label ignores the properties above. // @property(nullabel, nonatomic,copy) NSAttributedString *attributedText NS_AVAIlabel_IOS(6_0); // default is nil(富文本,圖文混排爲這個類型) // // // the 'highlight' property is used by subclasses for such things as pressed states. it's useful to make it part of the base class as a user property // // @property(nullabel, nonatomic,strong) UIColor *highlightedTextColor; // default is nil(高亮文字的顏色) // @property(nonatomic,getter=isHighlighted) BOOL highlighted; // default is NO // // @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // default is NO // @property(nonatomic,getter=isEnabled) BOOL enabled; // default is YES. changes how the label is drawn // // // this determines the number of lines to draw and what to do when sizeToFit is called. default value is 1 (single line). A value of 0 means no limit // // if the height of the text reaches the # of lines or the height of the view is less than the # of lines allowed, the text will be // // truncated using the line break mode. // // @property(nonatomic) NSInteger numberOfLines; // // // these next 3 property allow the label to be autosized to fit a certain width by scaling the font size(s) by a scaling factor >= the minimum scaling factor // // and to specify how the text baseline moves when it needs to shrink the font. // 文字本身調整大小去適應label的寬度 // @property(nonatomic) BOOL adjustsFontSizeToFitWidth; // default is NO // 文字的基線(下面有介紹) // @property(nonatomic) UIBaselineAdjustment baselineAdjustment; // default is UIBaselineAdjustmentAlignBaselines // 文字最小的縮小倍數 // @property(nonatomic) CGFloat minimumScaleFactor NS_AVAIlabel_IOS(6_0); // default is 0.0 // // // Tightens inter-character spacing in attempt to fit lines wider than the availabel space if the line break mode is one of the truncation modes before starting to truncate. // // The maximum amount of tightening performed is determined by the system based on contexts such as font, line width, etc. // @property(nonatomic) BOOL allowsDefaultTighteningForTruncation NS_AVAIlabel_IOS(9_0); // default is NO // // // override points. can adjust rect before calling super. // // label has default content mode of UIViewContentModeRedraw // // - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines; // - (void)drawTextInRect:(CGRect)rect; // // // // Support for constraint-based layout (auto layout) // // If nonzero, this is used when determining -intrinsicContentSize for multiline labels // @property(nonatomic) CGFloat preferredMaxLayoutWidth NS_AVAIlabel_IOS(6_0); // // // // deprecated: // // @property(nonatomic) CGFloat minimumFontSize NS_DEPRECATED_IOS(2_0, 6_0) __TVOS_PROHIBITED; // deprecated - use minimumScaleFactor. default is 0.0 // // // Non-functional. Hand tune by using NSKernAttributeName to affect tracking, or consider using the allowsDefaultTighteningForTruncation property. // @property(nonatomic) BOOL adjustsLetterSpacingToFitWidth NS_DEPRECATED_IOS(6_0,7_0) __TVOS_PROHIBITED; UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(0, 20, 100, 50); label.backgroundColor = [UIColor orangeColor]; label.text = @"But thanks to last autumn's"; // label.shadowOffset = CGSizeMake(10, 10); //文字陰影偏移量 // label.shadowColor = [UIColor redColor]; //文字陰影顏色 //文本文字自適應大小 label.adjustsFontSizeToFitWidth = YES; // label.baselineAdjustment = UIBaselineAdjustmentAlignCenters; //當adjustsFontSizeToFitWidth=YES時候,若是文本font要縮小時 //baselineAdjustment這個值控制文本的基線位置,只有文本行數爲1是有效 //有三種方式 //typedef enum { // UIBaselineAdjustmentAlignBaselines = 0, 默認值文本最上端於label中線對齊 // UIBaselineAdjustmentAlignCenters,//文本中線於label中線對齊 // UIBaselineAdjustmentNone,//文本最低端與label中線對齊 //} UIBaselineAdjustment; // label.minimumFontSize = 12; // ios6.0及之後使用minimumScaleFactor // label.minimumScaleFactor = 0.9; //文字自適應寬度,最小的縮小倍數 // label.adjustsLetterSpacingToFitWidth = YES; //控制文字間的間距適應寬度 9.0及之後使用下面的 allowsDefaultTighteningForTruncation // label.allowsDefaultTighteningForTruncation = YES; // CGRect rect = [label textRectForBounds:CGRectMake(0, 20, 180, 100) limitedToNumberOfLines:0]; // // NSLog(@"rect %@",NSStringFromCGRect(rect)); // 自動佈局 // label.preferredMaxLayoutWidth = 50; label.numberOfLines = 0; // 顯示多少行,0爲自動換行 [self.view addSubview:label]; }