本文总结自 iOS核心动画总结
该系列更多文章点击
#import "ViewController.h"
#import <CoreText/CoreText.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *labelView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self showSomeTextWithAttributr];
}
- (void)showSomeTextWithAttributr {
CATextLayer *textLayer = [CATextLayer layer];
textLayer.frame = _labelView.bounds;
textLayer.contentsScale = [UIScreen mainScreen].scale;
[_labelView.layer addSublayer:textLayer];
textLayer.alignmentMode = kCAAlignmentJustified;
textLayer.wrapped = YES;
UIFont *font = [UIFont systemFontOfSize:20];
NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque massa arcu, eleifend vel varius in, facilisis pulvinar leo. Nunc quis nunc at mauris pharetra condimentum ut ac neque. Nuncelementum, libero ut porttitor dictum, diam odio congue lacus, velfringilla sapien diam at purus. Etiam suscipit pretium nunc sit amet lobortis";
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text];
CFStringRef fontName = (__bridge CFStringRef)font.fontName;
CGFloat fontSize = font.pointSize;
CTFontRef fontRef = CTFontCreateWithName(fontName, fontSize, NULL);
NSDictionary *attr = @{(__bridge id)kCTForegroundColorAttributeName : (__bridge id)[UIColor redColor].CGColor,
(__bridge id)kCTFontAttributeName : (__bridge id) fontRef};
[string setAttributes:attr range:NSMakeRange(0, text.length)];
attr = @{(__bridge id)kCTForegroundColorAttributeName : (__bridge id)[UIColor yellowColor].CGColor,
(__bridge id)kCTFontAttributeName : (__bridge id)fontRef,
(__bridge id)kCTUnderlineStyleAttributeName : @(kCTUnderlineStyleThick)};
[string setAttributes:attr range:NSMakeRange(4, 40)];
CFRelease(fontRef);
textLayer.string = string;
}
- (void)showSomeText{
CATextLayer *textLayer = [CATextLayer layer];
textLayer.frame = _labelView.bounds;
[self.labelView.layer addSublayer:textLayer];
textLayer.foregroundColor = [UIColor blueColor].CGColor;
textLayer.alignmentMode = kCAAlignmentJustified;
textLayer.truncationMode = kCATruncationEnd;
textLayer.wrapped = YES;
textLayer.contentsScale = [UIScreen mainScreen].scale;
UIFont *font = [UIFont systemFontOfSize:15];
CFStringRef fontName = (__bridge CFStringRef)font.fontName;
CGFontRef fontRef = CGFontCreateWithFontName(fontName);
textLayer.font = fontRef;
textLayer.fontSize = font.pointSize;
CGFontRelease(fontRef);
NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque massa arcu, eleifend vel varius in, facilisis pulvinar leo. Nunc quis nunc at mauris pharetra condimentum ut ac neque. Nunc elementum, libero ut porttitor dictum, diam odio congue lacus, vel fringilla sapien diam at purus. Etiam suscipit pretium nunc sit amet lobortis";
textLayer.string = text;
}
文章中有部分的需要解释的稍后在编辑,有什么疑问也可以在评论留言
demo