CoreText 简单 使用

 1 - (void)drawRect:(CGRect)rect
 2 {
 3     NSString *longText = @"CoreText"; /* ... */
 4     NSRange rang =[self.hightString rangeOfString:longText];
 5     longText = @"CoreText";
 6      NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
 7                                          initWithString:longText];
 8 
 9     
10     if (rang.location == NSNotFound)
11     {
12        
13     }
14     
15        
16     // make a few words bold
17     CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), 14.0, NULL);
18     CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 14.0, NULL);
19     
20     [string addAttribute:(id)kCTFontAttributeName
21                    value:(__bridge id)helvetica
22                    range:NSMakeRange(0, [string length])];
23     
24     
25     [string addAttribute:(id)kCTForegroundColorAttributeName
26                    value:(id)[UIColor redColor].CGColor
27                    range:NSMakeRange(3, 5)];
28     
29     // layout master
30     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(
31                                                                            (CFAttributedStringRef)string);
32     
33     // left column form
34     CGMutablePathRef leftColumnPath = CGPathCreateMutable();
35     CGPathAddRect(leftColumnPath, NULL,
36                   CGRectMake(0, 0,
37                              self.bounds.size.width/2.0,
38                              self.bounds.size.height));
39     
40     // left column frame
41     CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,
42                                                     CFRangeMake(0, 0),
43                                                     leftColumnPath, NULL);
44     
45     // right column form
46     CGMutablePathRef rightColumnPath = CGPathCreateMutable();
47     CGPathAddRect(rightColumnPath, NULL,
48                   CGRectMake(self.bounds.size.width/2.0, 0,
49                              self.bounds.size.width/2.0,
50                              self.bounds.size.height));
51     
52     NSInteger rightColumStart = CTFrameGetVisibleStringRange(leftFrame).length;
53     
54     // right column frame
55     CTFrameRef rightFrame = CTFramesetterCreateFrame(framesetter,
56                                                      CFRangeMake(rightColumStart, 0),
57                                                      rightColumnPath,
58                                                      NULL);
59     
60     // flip the coordinate system
61     CGContextRef context = UIGraphicsGetCurrentContext();
62     CGContextSetTextMatrix(context, CGAffineTransformIdentity);
63     CGContextTranslateCTM(context, 0, self.bounds.size.height);
64     CGContextScaleCTM(context, 1.0, -1.0);
65     
66     // draw
67     CTFrameDraw(leftFrame, context);
68     CTFrameDraw(rightFrame, context);
69     
70     // cleanup
71     CFRelease(leftFrame);
72     CGPathRelease(leftColumnPath);
73     CFRelease(rightFrame);
74     CGPathRelease(rightColumnPath);
75     CFRelease(framesetter);
76     CFRelease(helvetica);
77     CFRelease(helveticaBold);
78    // [string release];
79     
80 }
CoreText

 

posted @ 2015-03-28 15:40  ifgyong  阅读(133)  评论(0编辑  收藏  举报