- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context , CGAffineTransformIdentity);
CGContextTranslateCTM(context , 0 ,self.bounds.size.height);
CGContextScaleCTM(context, 1.0 ,-1.0);
NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:@"富文本学习/慢慢来吧。不是一朝一夕之功。ok n ok ok"];
CTRunDelegateDeallocateCallback deallocateCallback= CTRunDelegateDeallocateCallbackFunction;
NSLog(@"CTRunDelegateGetAscentCallback 获取ascent");
CTRunDelegateGetAscentCallback ascentCallback= CTRunDelegateGetAscentCallbackFunction;
CTRunDelegateGetDescentCallback descentCallback = CTRunDelegateGetDescentCallbackFunction;
CTRunDelegateGetWidthCallback widthCallBack = CTRunDelegateGetWidthCallbackFunction;
CTRunDelegateCallbacks callBack ;
callBack.version =kCTRunDelegateCurrentVersion;
callBack.dealloc = deallocateCallback;
callBack.getAscent = ascentCallback;
callBack.getDescent = descentCallback;
callBack.getWidth = widthCallBack;
CTRunDelegateRef delegate= CTRunDelegateCreate(&callBack, NULL);
NSDictionary * dic =[NSDictionary dictionaryWithObject:(__bridge id)delegate forKey:(__bridge NSString * )kCTRunDelegateAttributeName];
unichar placeHolder = 0xFFFC;///占位符
NSString * placeHolderStr = [NSString stringWithCharacters:&placeHolder length:1];
NSAttributedString * att = [[NSAttributedString alloc]initWithString:placeHolderStr attributes:dic];
[mabstring insertAttributedString:att atIndex:3 ];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)mabstring);
CGMutablePathRef Path = CGPathCreateMutable();
CGPathAddRect(Path, NULL ,CGRectMake(10 , 0 ,self.bounds.size.width-10 , self.bounds.size.height-60));
CFRange range = CFRangeMake(0, 0);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,range, Path, NULL);
CTFrameDraw(frame,context);
CGPathRelease(Path);
CFRelease(framesetter);
}
近期评论