@property(nullable,nonatomic,copy) NSString *text;//设置和读取文本内容,默认为nil
@property(null_resettable,nonatomic,strong)UIFont *font;//设置字体大小,默认17
self.label=[UIFont systemFontOfSize:20];
self.label=[UIFont boldSystemFontOfSize:20]; //加粗方法
self.label=[UIFont fontWithName:@"Arial" size:16]; //指定字体和大小
@property(null_resettable, nonatomic,strong)UIColor *textColor;//设置文字颜色,默认为黑色
@property(nullable,nonatomic,strong)UIColor *shadowColor; //阴影的颜色
@property(nonatomic) CGSize shadowOffset;//阴影面积的大小
@property(nonatomic) NSTextAlignment textAlignment;/设置标签文本对齐方式。
self.label.textAlignment= NSTextAlignmentLeft 向左对齐
self.label.textAlignment= NSTextAlignmentCenter居中对齐
self.label.textAlignment= NSTextAlignmentRight 向右对齐
@property(nonatomic) NSLineBreakMode lineBreakMode;
self.label.lineBreakMode=NSLineBreakByTruncatingTail;结尾部分的内容
以……方式省略,显示前面的文字内容。
NSLineBreakByWordWrapping//以单词为显示单位显示,后面部分省略不显示。
NSLineBreakByCharWrapping,//以字符为显示单位显示,后面部分省略不显示
NSLineBreakByClipping,//剪切与文本宽度相同的内容长度,后半部分被删除。
NSLineBreakByTruncatingHead,//前面部分文字以……方式省略,显示尾部文字内容。
NSLineBreakByTruncatingMiddle//中间的内容以……方式省略,显示头尾的文字内容。
@property(nullable,nonatomic,copy) NSAttributedString *attributedTextNS_AVAILABLE_IOS(6_0); //
@property(nullable,nonatomic,strong) UIColor *highlightedTextColor;//高亮状态的文字颜色
@property(nonatomic,getter=isHighlighted)BOOL highlighted; //是否处于高亮状态,默认为no
@property(nonatomic,getter=isUserInteractionEnabled)BOOL userInteractionEnabled; //人机交互是否打开,默认为NO
@property(nonatomic,getter=isEnabled) BOOL enabled; //文本是否可编辑,默认可以。
@property(nonatomic)NSInteger numberOfLines;文本显示的行数,默认为1,等于0时是不限制行数
@property(nonatomic)BOOL adjustsFontSizeToFitWidth;//设置字体大小适应label宽度,默认为NO
@property(nonatomic)UIBaselineAdjustment baselineAdjustment; // 文本对齐方式
// UIBaselineAdjustmentAlignBaselines = 0, 默认值文本最上端于label中线对齐
// UIBaselineAdjustmentAlignCenters,//文本中线于label中线对齐
// UIBaselineAdjustmentNone,//文本最低端与label中线对齐
@property(nonatomic)CGFloat minimumScaleFactorNS_AVAILABLE_IOS(6_0);//设置最小收缩比例,如果Label宽度小于文字长度时,文字进行收缩,收缩超过比例后,停止收缩.默认为0.
@property(nonatomic)BOOL allowsDefaultTighteningForTruncationNS_AVAILABLE_IOS(9_0);//这个属性是用来设置label的最大宽度的
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;//用来改变label里面文字展示窗口的大小,你可以自己根据文字的多少,来计算窗口的大小
- (void)drawTextInRect:(CGRect)rect; //需要重载此方法,然后由子类调用,重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了。
@property(nonatomic)CGFloat preferredMaxLayoutWidthNS_AVAILABLE_IOS(6_0);//这个属性是用来设置多行label的最大宽度的;当自动布局的时候约束这个label的时候这个属性会起作用;在自动布局添加约束中,若文本超过了指定的最大宽度的时候 文本会另起一行 从而增加了label的高度
@property(nonatomic)CGFloat minimumFontSizeNS_DEPRECATED_IOS(2_0,6_0)__TVOS_PROHIBITED; // 字体最小型号
@property(nonatomic)BOOL adjustsLetterSpacingToFitWidthNS_DEPRECATED_IOS(6_0,7_0)__TVOS_PROHIBITED;//改变字母间的间距来适应label的大小
@end
label的常用总结
UILabel有两个接口是专门用来重写,以此来自定义自己的label,如下:
Drawing and Positioning Overrides
– textRectForBounds:limitedToNumberOfLines:
– drawTextInRect:
上面这两个方法不是用来调用的,只适合被UILabel子类重写
– textRectForBounds:limitedToNumberOfLines:
用来改变label里面文字展示窗口的大小,你可以自己根据文字的多少,来计算窗口的大小
– drawTextInRect:
在绘图环境实现文字的绘制,这个方法里面里面已经配置好了绘图环境,使用方式如下:
1.直接获得当前绘图上下文,
2.接着更改绘图环境设置
3.在就是调用super方法来绘制即可
Label添加边框
titleLabel.layer.borderColor = [[UIColor grayColor] CGColor];
titleLabel.layer.borderWidth = 2;
Label的N行后的frame
heightLabel = [myLabel textRectForBounds:bounds
limitedToNumberOfLines:20]; //计算20行后的Label的Frame
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)];
attributedText:设置标签属性文本(富文本)。
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;