Original Sticker Address: http://hi.baidu.com/jwq359699768/blog/item/5df305c893413d0a7e3e6f7b.html
Core text This package is not the default, you have to manually add it in.
Several key points for composing text in iOS using the core text are as follows:
Word spacing: kctkernattributename
Line spacing: kctparagraphstylespecifierlinespacingadjustment or kctparagraphstylespecifierlinespacing (deprecated)
Segment spacing: kctparagraphstylespecifierparagraphspacing
Text alignment: Kctparagraphstylespecifieralignment;
The other thing is that the core text shows the word is upside down, when used to flip the following:
Cgcontextref context = Uigraphicsgetcurrentcontext ();
Cgcontextsettextmatrix (context,cgaffinetransformidentity);
CGCONTEXTTRANSLATECTM (Context,0,self.bounds.size.height);
CGCONTEXTSCALECTM (context,1.0,-1.0);
The last point to note is that the carriage return under the MAC is not the same as Windows, the carriage return under Windows is composed of \ r \ n and the Mac has only one \ n, so if it is not removed at the end of each paragraph will have a blank line, the method is removed as follows:
NSString *mystring = [labelstring stringbyreplacingoccurrencesofstring: "\ r \ n" withstring: "\ n"];
The specific code is implemented as follows:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface Textlayoutlabel:uilabel
{
@private
CGFloat Characterspacing_; Word spacing
@private
Long Linesspacing_; Line spacing
}
@property (nonatomic,assign) cgfloat characterspacing;
@propery (nonatomic,assign) long linesspacing;
@end
#import "TextLayoutLabel.h"
#import <CoreText/CoreText.h>
@implementation Textlayoutlabel
@synthesize characterspacing = characterspacing_;
@synthesize linesspacing = linesspacing_;
-(ID) initWithFrame: (CGRect) frame
{//Initialize word spacing, line spacing
if (self =[super initwithframe:frame])
{
self.characterspacing = 2.0f;
self.linesspacing = 5.0f;
}
return self;
}
-(void) setcharacterspacing: (cgfloat) characterspacing//External call set word spacing
{
Characterspacing_ = characterspacing;
[Self setneedsdisplay];
}
-(void) Setlinesspacing: (long) linesspacing//External call set line spacing
{
Linesspacing_ = linesspacing;
[Self setneedsdisplay];
}
-(void) Drawtextinrect: (CGRect) Requestedrect
{
Remove empty lines
NSString *labelstring = Self.text;
NSString *mystring = [labelstring stringbyreplacingoccurrencesofstring:@ "\ r \ n" withstring: "\ n"];
Create AttributeString
Nsmutableattributedstring *string =[[nsmutableattributedstring Alloc]initwithstring:self.text];
Set font and size
Ctfontref Helveticabold = Ctfontcreatewithname ((cfstringref) self.font.fontname,self.font.pointsize,null);
[String AddAttribute: (ID) kctfontattributename value: (ID) helveticabold range:nsmakerange (0,[string length])];
Set word spacing
if (self.characterspacing)
{
Long number = self.characterspacing;
Cfnumberref num = cfnumbercreate (kcfallocatordefault,kcfnumbersint8type,&number);
[String AddAttribute: (ID) kctkernattributename value: (ID) num rang:nsmakerange (0,[string length])];
Cfrelease (num);
}
Set Font Color
[String AddAttribute: (ID) kctforegroundcolorattributename value: (ID) (self.textColor.CGColor) Range:nsmakerange (0,[ string length]);
Create text alignment
cttextalignment alignment = Kctlefttextalignment;
if (self.textalignment = = Uitextalignmentcenter)
{
Alignment = Kctcentertextalignment;
}
if (self.textalignment = = uitextalignmentright)
{
Alignment = Kctrighttextalignment;
}
Ctparagraphstylesetting Alignmentstyle;
Alignmentstyle.spec = kctparagraphstylespecifieralignment;
alignmentstyle.valuesize = sizeof (alignment);
Alignmentstyle.value = &alignment;
Set Text line spacing
CGFloat linespace = self.linesspacing;
Ctparagraphstylesetting Linespacestyle;
Linespacestyle.spec = kctparagraphstylespecifierlinespacingadjustment;
linespacestyle.valuesize = sizeof (linespace);
Linespacestyle.value =&linespace;
Set Text segment Spacing
CGFloat paragraphspacing = 5.0;
Ctparagraphstylesetting Paragraphspacestyle;
Paragraphspacestyle.spec = kctparagraphstylespecifierparagraphspacing;
paragraphspacestyle.valuesize = sizeof (cgfloat);
Paragraphspacestyle.value = ¶graphSpacing;
Creating an array of settings
Ctparagraphstylesetting settings[] ={alignmentstyle,linespacestyle,paragraphspacestyle};
Ctparagraphstyleref style = ctparagraphstylecreate (settings, sizeof (settings));
Add settings to Text
[String AddAttribute: (ID) kctparagraphstyleattributename value: (ID) style range:nsmakerange (0, [string length])];
Typesetting
Ctframesetterref Framesetter = ctframesettercreatewithattributedstring ((cfattributedstringref) string);
Cgmutablepathref Leftcolumnpath = cgpathcreatemutable ();
Cgpathaddrect (Leftcolumnpath, NULL, CGRectMake (0, 0, self.bounds.size.width, self.bounds.size.height));
Ctframeref leftframe = ctframesettercreateframe (framesetter,cfrangemake (0, 0), Leftcolumnpath, NULL);
Flip the coordinate system (the text turns upside down)
Cgcontextref context = Uigraphicsgetcurrentcontext ();
Cgcontextsettextmatrix (context, cgaffinetransformidentity);
CGCONTEXTTRANSLATECTM (context, 0, self.bounds.size.height);
CGCONTEXTSCALECTM (Context, 1.0,-1.0);
Draw the text
Ctframedraw (Leftframe,context);
Release
Cgpathrelease (Leftcolumnpath);
Cfreleale (Framesetter);
Cfrelease (Helveticabold);
[String release];
Uigraphicspushcontext (context);
}
@end
iOS uses the core text to format the text-go