首页 > 技术文章 > 第一篇 UILabel

kinghx 2016-03-15 18:43 原文

 

 

- (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]; }

 

推荐阅读