CoreText简单设置

UILabel * textLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 100)];

textLabel.font = [UIFont systemFontOfSize:20];

textLabel.textColor = [UIColor blackColor];

textLabel.textAlignment = NSTextAlignmentLeft;

[self.view addSubview:textLabel];

NSString * str = @"this is a text hehe";

//创建富文本对象

NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc]initWithString:str];

//设置字号

CTFontRef font1 = CTFontCreateWithName(CFSTR("Georgia"), 40, NULL);

//__bridge表示在ARC环境下,OC对象和CF对象的桥接,CF即Core Fondation,包含CoreText ,CoreImage,CoreGraphics,CoreAnimation等系统框架,主要负责OC和CF类型的转换

[attributedString addAttribute:(id)kCTFontAttributeName value:(__bridge id _Nonnull)(font1) range:NSMakeRange(0, 4)];

//设置斜体字

CTFontRef font2 = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:40].fontName, 40, NULL);

[attributedString addAttribute:(id)kCTFontAttributeName value:(__bridge id _Nonnull)(font2) range:NSMakeRange(0, 4)];

//设置下划线(默认为黑色)

[attributedString addAttribute:(id)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)];

//设置下划线颜色

[attributedString addAttribute:NSUnderlineColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];

//设置字体颜色

[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 4)];

//设置字体间隔

long number1 = 10;

CFNumberRef num1 = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &number1);

[attributedString addAttribute:(id)kCTKernAttributeName value:(__bridge id _Nonnull)(num1) range:NSMakeRange(5,6)];

//设置空心字

long number2 = 2;

CFNumberRef num2 = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &number2);

[attributedString addAttribute:(id)kCTStrokeWidthAttributeName value:(__bridge id _Nonnull)(num2) range:NSMakeRange(0, str.length)];

textLabel.attributedText = attributedString;

你可能感兴趣的:(CoreText简单设置)