声明:此为本人学习笔记,若有纰漏错误之处的可留言共同探讨
UILabel基本用法:
// 1创建UIlabel
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.window.frame.size.width/5, self.window.frame.size.height/2, 200, 50)]; // 标签的大小设置
// 添加子视图
[self.window addSubview:label]; // Sub 子
// 2添加文字
label.text = @"hhsdfsfdfsdfdssfsdfsdfsfsdffg";
// 3文字颜色
label.textColor =[UIColor grayColor];
// 4字体阴影
label.shadowColor = [UIColor blueColor];
// 5字体阴影偏移量
label.shadowOffset =CGSizeMake(1, 1);
// 6标签的背景颜色
label.backgroundColor=[UIColor whiteColor];
// 7字体大小
label.font = [[UIFont alloc]fontWithSize:3.2];
label.font =[UIFont systemFontOfSize:38];
// 8外框
label.layer.borderWidth=2; // layer 图层
// 9圆角
label.layer.cornerRadius = 14;
// 10文本对齐方式
[label setTextAlignment:NSTextAlignmentCenter];
// 故事板的对齐方式
// 11文本省略模式
[label setLineBreakMode:NSLineBreakByTruncatingMiddle];
// 12换行
label.numberOfLines = 3;
上面是基本用法,下面使用一个中高级知识点:让单个Label呈现不同的颜色、样式。(实现的myLabel源码在后面, Demo源码在最后)
下面是实现的关键源码
#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>
@interface myLabel : UILabel{
}
@property (nonatomic,retain) NSMutableAttributedString *attString;
// 设置字的颜色
- (void)setMyFontColor:(UIColor *)color withIndex:(NSInteger)index withLength:(NSInteger)length;
// 设置字体
- (void)setMyFont:(UIFont *)font withIndex:(NSInteger)index withLength:(NSInteger)length;
// 设置字的样式
- (void)setMyFontStyle:(CTUnderlineStyle)style withIndex:(NSInteger)index withLength:(NSInteger)length;
@end
#import "myLabel.h"
@implementation myLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 自定义初始化
}
return self;
}
- (void)drawRect:(CGRect)rect{
// 用于渲染一个无格式或属性文本字符
// 最主要的特点是CATextLayer可以被NSMutableAttributedString直接附值。而NSMutableAttributedString有可以最自己内容作出颜色以及大小的调整。
CATextLayer *textLayer = [CATextLayer layer];
textLayer.string = self.attString;
textLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self.layer addSublayer:textLayer];
/* 原理
contentScale属性,用来决定图层内容应该以怎样的分辨率来渲染。contentsScale并不关心屏幕的拉伸因素而总是默认为1.0。
*/
textLayer.contentsScale = [UIScreen mainScreen].scale; // 如果没有加这句 文本会有像素化现象。这是因为并没有以Retina的方式渲染
}
// 重写系统方法
- (void)setText:(NSString *)text{
[super setText:text];
if (text == nil) {
self.attString = nil;
}else{
self.attString = [[NSMutableAttributedString alloc] initWithString:text];
}
}
// 设置字的颜色
- (void)setMyFontColor:(UIColor *)color withIndex:(NSInteger)index withLength:(NSInteger)length{
if (index < 0||index>self.text.length-1||length+index>self.text.length) {
return;
}
[self.attString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)color.CGColor range:NSMakeRange(index, length)];
}
// 设置字体
- (void)setMyFont:(UIFont *)font withIndex:(NSInteger)index withLength:(NSInteger)length{
if (index < 0||index>self.text.length-1||length+index>self.text.length) {
return;
}
[self.attString addAttribute:(NSString *)kCTFontAttributeName
value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName,font.pointSize,NULL)) range:NSMakeRange(index, length)];
}
// 设置字的样式
- (void)setMyFontStyle:(CTUnderlineStyle)style withIndex:(NSInteger)index withLength:(NSInteger)length{
if (index < 0||index>self.text.length-1||length+index>self.text.length) {
return;
}
[self.attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:style] range:NSMakeRange(index, length)];
}
@end