初探coreText

参考 http://www.dev3g.com/?p=27
对于画文本可以很多做过iphone开发的人员都曾为bojective-c语言的设计而苦恼。前几天我也因为文本的排版很纠结。看了很多上架了的软件一样有此类问题,这虽不伤产品功能,但很伤大雅。 初探coreText_第1张图片
用默认的排版方式根本不能使左右两边始终对齐。后来找到了coretext可以解决这样的问题。然后救在网上搜到了很好的资料http://www.dev3g.com/?p=27。现在我遇到的问题勉强算是解决了,但对于coretext了解还甚少,还有就是coretext里面的东西都是用C写的,我对C就大一那么点知识,现在也还没有那么多的时间针对学习C(不过我想不久就有了)。需要补充的知识很多很多啊!哦,解决后 初探coreText_第2张图片
是不是对齐了。

对了,coretext是直接在view上面draw的,而怎么达到textview的效果呢,下面我会讲到





 //创建要输出的字符串f

        NSString *longText = @"自己添加文本";

        //创建AttributeStringfdsa

        NSMutableAttributedString *string = [[NSMutableAttributedString alloc]

                                             initWithString:longText];

        

        //创建字体以及字体大小

        CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), 14.0NULL);

        CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 14.0,NULL);

        //添加字体目标字符串从下标0开始到字符串结尾

        [string addAttribute:(id)kCTFontAttributeName

                       value:(id)helvetica

                       range:NSMakeRange(0, [string length])];

        

        //添加字体目标字符串从下标0开始,截止到4个单位的长度

        [string addAttribute:(id)kCTFontAttributeName

           value:(id)helveticaBold

           range:NSMakeRange(04)];

        

        //添加字体目标字符串从下标6开始,截止到5个单位长度

        [string addAttribute:(id)kCTFontAttributeName

           value:(id)helveticaBold

           range:NSMakeRange(65)];

        

        //添加字体目标字符串从下标109开始,截止到9个单位长度

        [string addAttribute:(id)kCTFontAttributeName

           value:(id)helveticaBold

           range:NSMakeRange(1099)];

        

        //添加字体目标字符串从下标223开始,截止到6个单位长度

        [string addAttribute:(id)kCTFontAttributeName

           value:(id)helveticaBold

           range:NSMakeRange(2236)];

        

        //添加颜色,目标字符串从下标0开始,截止到4个单位长度

        [string addAttribute:(id)kCTForegroundColorAttributeName

           value:(id)[UIColor blueColor].CGColor

           range:NSMakeRange(04)];

        

        //添加过程同上

        [string addAttribute:(id)kCTForegroundColorAttributeName

         value:(id)[UIColor redColor].CGColor

           range:NSMakeRange(183)];

        

       [string addAttribute:(id)kCTForegroundColorAttributeName

          value:(id)[UIColor greenColor].CGColor

          range:NSMakeRange(6576)];

        

       [string addAttribute:(id)kCTForegroundColorAttributeName

          value:(id)[UIColor blueColor].CGColor

           range:NSMakeRange(1536)];

        

        //创建文本对齐方式

        CTTextAlignment alignment = kCTJustifiedTextAlignment;//这种对齐方式会自动调整,使左右始终对齐

        CTParagraphStyleSetting alignmentStyle;

        alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;//指定为对齐属性

        alignmentStyle.valueSize=sizeof(alignment);

        alignmentStyle.value=&alignment;

        

        //创建文本行间距

        CGFloat lineSpace=10.0f;//间距数据

        CTParagraphStyleSetting lineSpaceStyle;

        lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;//指定为行间距属性

        lineSpaceStyle.valueSize=sizeof(lineSpace);

        lineSpaceStyle.value=&lineSpace;

        

        //创建样式数组

        CTParagraphStyleSetting settings[]={

            alignmentStyle,lineSpaceStyle

        };

        

        //设置样式

        CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings,sizeof(settings));

        

        //给字符串添加样式attribute

        [string addAttribute:(id)kCTParagraphStyleAttributeName

                       value:(id)paragraphStyle

                       range:NSMakeRange(0, [string length])];

        

        // layout master

        framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);

//计算文本绘制size

        CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter,CFRangeMake(0,0), NULL, CGSizeMake(385, 400 * 10), NULL); 

    //创建textBoxSize以设置view的frame

        CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height +1);

        NSLog(@"textBoxSize  == %f,%f,%f",textBoxSize.width,textBoxSize.height,textBoxSize.width / textBoxSize.height);

        self.frame = CGRectMake(60, textBoxSize.width , textBoxSize.height);

        [string release];


//- (void)drawRect:(CGRect)rect;代码

 

 CGMutablePathRef leftColumnPath = CGPathCreateMutable();

    CGPathAddRect(leftColumnPath, NULL,

                  CGRectMake(00,

                             self.bounds.size.width,

                             self.bounds.size.height));

    

    CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,

                                         CFRangeMake(00),

                                         leftColumnPath, NULL);

    

    NSLog(@"textBoxSize  == %f,%f,%f",self.frame.size.width,self.frame.size.height);

// flip the coordinate system

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetTextMatrix(context, CGAffineTransformIdentity);

CGContextTranslateCTM(context, 0, self.bounds.size.height);

CGContextScaleCTM(context, 1.0, -1.0);

 

// draw

CTFrameDraw(leftFrame, context);

 

// cleanup

 

CGPathRelease(leftColumnPath);

CFRelease(framesetter);

//CFRelease(helvetica);

// CFRelease(helveticaBold);

 

UIGraphicsPushContext(context);



然后可以将这个view添加到scrollview里面就ok了,跟textview一样有滑动效果,但样式漂亮多了


你可能感兴趣的:(初探coreText)