【学习笔记】UILabel

来源:互联网 发布:大数据平台架构图深度 编辑:程序博客网 时间:2024/06/03 21:01

//  UILabel.h

 

#import <Foundation/Foundation.h>

#import <CoreGraphics/CoreGraphics.h>

#import <UIKit/UIView.h>

#import <UIKit/UIStringDrawing.h>

#import <UIKit/UIKitDefines.h>

@class UIColor, UIFont;

NS_CLASS_AVAILABLE_IOS(2_0)@interfaceUILabel :UIView <NSCoding>

 

UILabel *label = [[UILabel alloc] initWithFrame:CGR];

//创建label1,在某个位置


@property(nonatomic,copy)  NSString           *text;           

//文本内容,默认为空   

label.text=@”aaaaa”; 


@property(nonatomic,retain)UIFont             *font;   

//字体大小,默认为17
label.font = [UIFont systemFontOfSize:20]; //⼀一般方法
label.font = [UIFont boldSystemFontOfSize:20]; //加粗方法
label.font = [UIFont fontWithName:@"Arial" size:16]; //指定


@property(nonatomic,retain)UIColor            *textColor;     

//字体颜色,默认黑色              
label.textColor = [UIColor whiteColor];

 

@property(nonatomic,retain)UIColor            *shadowColor;   

//阴影颜色
[label setShadowColor:[UIColor blackColor]];

 

@property(nonatomic)       CGSize             shadowOffset;   

//阴影大小方向
[label setShadowOffset:CGSizeMake(-1, -1)];


@property(nonatomic)       NSTextAlignment    textAlignment;  

//文本对齐方式,默认左对齐
label.textAlignment = NSTextAlignmentCenter; //还有NSTextAlignmentLeft、 NSTextAlignmentRight.

 

@property(nonatomic)       NSLineBreakMode    lineBreakMode;  

// lineBreakMode //设置文字过长时的显示格式,默认后面.....       

label.lineBreakMode = NSLineBreakByCharWrapping;以字符为显示单位显示,后面部分省略不显示。

label.lineBreakMode = NSLineBreakByClipping;剪切与文本宽度相同的内容长度,后半部分被删除。

label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字以……方式省略,显示尾部文字内容。

label.lineBreakMode = NSLineBreakByTruncatingMiddle;中间的内容以……方式省略,显示头尾的文字内容。

label.lineBreakMode = NSLineBreakByTruncatingTail;结尾部分的内容以……方式省略,显示头的文字内容。

label.lineBreakMode = NSLineBreakByWordWrapping;以单词为显示单位显示,后面部分省略不显示。


@property(nonatomic,copy)  NSAttributedString*attributedTextNS_AVAILABLE_IOS(6_0); 

设置标签属性文本。 NSString *text = @"first"; NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:text]; [textLabelStr setAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(11,10)]; label.attributedText = textLabelStr;

 

@property(nonatomic,retain)              UIColor*highlightedTextColor;

//高亮时文字颜色,默认正常不变 label.highlightedTextColor = [UIColor orangeColor]; 

 

@property(nonatomic,getter=isHighlighted)BOOL    highlighted;         

//高亮 默认为NO       label.highlighted = YES;

 

@property(nonatomic,getter=isUserInteractionEnabled)BOOL userInteractionEnabled; // default is NO

 

 

@property(nonatomic,getter=isEnabled)               BOOLenabled;      

//是否可用,默认时YES,label.enable = NO;// 为NO将会使文本变暗,表示它没有激活,这时向它设置颜色值是无效的。

  

@property(nonatomic)NSIntegernumberOfLines;

//文本行数

label.numberOfLines = 2;

 

@property(nonatomic)BOOLadjustsFontSizeToFitWidth;        // default is NO

//控制文本基线,默认为NO,设为YES,可用baselineAdjustment,调整

 

@property(nonatomic)BOOLadjustsLetterSpacingToFitWidthNS_DEPRECATED_IOS(6_0,7_0);

//类似上述方法,不建议采用

@property(nonatomic)CGFloatminimumFontSizeNS_DEPRECATED_IOS(2_0,6_0);

//类似上述方法,不建议采用

 

@property(nonatomic)UIBaselineAdjustmentbaselineAdjustment;

// 控制文本基线,上下对齐方式,默认上对齐

label.baselineAdjustment = UIBaselineAdjustmentNone; UIBaselineAdjustmentAlignBaselines = 0,默认,文本最上端与中线对齐。 UIBaselineAdjustmentAlignCenters,  文本中线与label中线对齐。 UIBaselineAdjustmentNone, 文本最低端与label中线对齐。

 

@property(nonatomic)CGFloatminimumScaleFactorNS_AVAILABLE_IOS(6_0);// default is0.0

 

// override points. can adjust rect before callingsuper.

// label has default content mode ofUIViewContentModeRedraw

 

- (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)CGFloatpreferredMaxLayoutWidthNS_AVAILABLE_IOS(6_0);

 


@end

 

 

 

 

 

补充不确定

13、adjustsLetterSpacingToFitWidth //改变字母之间的间距来适应Label大小myLabel.adjustsLetterSpacingToFitWidth = NO;
 

15、 adjustsFontSizeToFitWidth//设置字体大小适应label宽度 

label.adjustsFontSizeToFitWidth = YES;

 
19、UILabel根据字数多少自动实现适应高度 UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 45, 0, 0)]; msgLabel.backgroundColor = [UIColor lightTextColor]; [msgLabel setNumberOfLines:0]; msgLabel.lineBreakMode = UILineBreakModeWordWrap; msgLabel.font = [UIFont fontWithName:@"Arial" size:12]; CGSize size = CGSizeMake(290, 1000); msgLabel.text = @"获取到的deviceToken,我们可以通过webservice服务提交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net应用环境中使用。"; CGSize msgSie = [msgLabel.text sizeWithFont:fonts constrainedToSize:size]; [msgLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)];20、渐变字体Label UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"btn.png"]]; NSString *title = @"Setting"; UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 44)]; titleLabel.textColor = titleColor; titleLabel.text = title; titleLabel.font = [UIFont boldSystemFontOfSize:20]; titleLabel.backgroundColor = [UIColor clearColor]; [self.view addSubview:titleLabel]; [titleLabel release];21、Label添加边框 titleLabel.layer.borderColor = [[UIColor grayColor] CGColor]; titleLabel.layer.borderWidth = 2;

 

0 0