CoreText使用


1.各パラメータを動的に調整しなくてもcharacterSpacing_を無視できるおよびlinesSpacing_.MartinCustomLabel.h

#import<Foundation/Foundation.h> #import<UIKit/UIKit.h> @interface MartinCustomLabel : UILabel { @private CGFloat characterSpacing_; // long linesSpacing_; // } @property(nonatomic,assign) CGFloat characterSpacing; @property(nonatomic,assign) long linesSpacing; /* * label */ - (int)getAttributedStringHeightWidthValue:(int)width; @end


2.行、字、段を動的に調整する必要がないので、コードには定数が書かれています.自分のニーズに合わせて変更できます.MartinCustomLabel.m

#import "MartinCustomLabel.h" #import<CoreText/CoreText.h> @interface MartinCustomLabel() { @private NSMutableAttributedString *attributedString; } - (void) initAttributedString; @end @implementation MartinCustomLabel @synthesize characterSpacing = characterSpacing_; @synthesize linesSpacing = linesSpacing_; -(id) initWithFrame:(CGRect)frame { // 、 if(self =[super initWithFrame:frame]) { self.characterSpacing = 1.5f; self.linesSpacing = 4.0f; } return self; } // -(void)setCharacterSpacing:(CGFloat)characterSpacing { characterSpacing_ = characterSpacing; [self setNeedsDisplay]; } // -(void)setLinesSpacing:(long)linesSpacing { linesSpacing_ = linesSpacing; [self setNeedsDisplay]; } /* * AttributedString */ - (void) initAttributedString { if(attributedString==nil){ // NSString *labelString = self.text; NSString *myString = [labelString stringByReplacingOccurrencesOfString:@"\r
"
withString:@"
"
]; // AttributeString attributedString =[[NSMutableAttributedString alloc]initWithString:myString]; // CTFontRef helveticaBold = CTFontCreateWithName((CFStringRef)self.font.fontName,self.font.pointSize,NULL); [attributedString addAttribute:(id)kCTFontAttributeName value:(id)helveticaBold range:NSMakeRange(0,[attributedString length])]; // long number = 1.5f; CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number); [attributedString addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(0,[attributedString length])]; CFRelease(num); /* if(self.characterSpacing) { long number = self.characterSpacing; CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number); [attributedString addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(0,[attributedString length])]; CFRelease(num); } */ // [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:(id)(self.textColor.CGColor) range:NSMakeRange(0,[attributedString length])]; // 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; // /* CGFloat lineSpace = self.linesSpacing; */ CGFloat lineSpace = 4.0f; CTParagraphStyleSetting lineSpaceStyle; lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment; lineSpaceStyle.valueSize = sizeof(lineSpace); lineSpaceStyle.value =&lineSpace; // CGFloat paragraphSpacing = 15.0; CTParagraphStyleSetting paragraphSpaceStyle; paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing; paragraphSpaceStyle.valueSize = sizeof(CGFloat); paragraphSpaceStyle.value = &paragraphSpacing; // CTParagraphStyleSetting settings[ ] ={alignmentStyle,lineSpaceStyle,paragraphSpaceStyle}; CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,3); // [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)style range:NSMakeRange(0 , [attributedString length])]; CFRelease(helveticaBold); } } /* * setText */ - (void) setText:(NSString *)text { [super setText:text]; [self initAttributedString]; } /* * */ -(void) drawTextInRect:(CGRect)requestedRect { [self initAttributedString]; // CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString); 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); // ( ) CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetTextMatrix(context , CGAffineTransformIdentity); CGContextTranslateCTM(context , 0 ,self.bounds.size.height); CGContextScaleCTM(context, 1.0 ,-1.0); // CTFrameDraw(leftFrame,context); // CGPathRelease(leftColumnPath); CFRelease(framesetter); UIGraphicsPushContext(context); } /* * label */ - (int)getAttributedStringHeightWidthValue:(int)width { [self initAttributedString]; int total_height = 0; CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString); //string NSAttributedString CGRect drawingRect = CGRectMake(0, 0, width, 100000); // CGMutablePathRef path = CGPathCreateMutable(); CGPathAddRect(path, NULL, drawingRect); CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL); CGPathRelease(path); CFRelease(framesetter); NSArray *linesArray = (NSArray *) CTFrameGetLines(textFrame); CGPoint origins[[linesArray count]]; CTFrameGetLineOrigins(textFrame, CFRangeMake(0, 0), origins); int line_y = (int) origins[[linesArray count] -1].y; // line y CGFloat ascent; CGFloat descent; CGFloat leading; CTLineRef line = (CTLineRef) [linesArray objectAtIndex:[linesArray count]-1]; CTLineGetTypographicBounds(line, &ascent, &descent, &leading); total_height = 100000 - line_y + (int) descent +1;//+1 descent int CFRelease(textFrame); return total_height; } @end


3.呼び出しステップの作成:

MartinCustomLabel *readNewsLable =[[MartinCustomLabel alloc] initWithFrame:CGRectZero]; readNewsLable.textColor = textCor; readNewsLable.lineBreakMode = UILineBreakModeWordWrap; readNewsLable.backgroundColor = [UIColor clearColor]; readNewsLable.font = TEXT_FONT_NAME([AccessData getNewsConnectFont]); [readNewsLable setText:text]; /* label frame */ [readNewsLable setFrame:CGRectMake(10, aboveY, WINDOW_W-20, [readNewsLable getAttributedStringHeightWidthValue:WINDOW_W-20])]; readNewsLable.numberOfLines = 0; [readNewsLable setTag:TEXT_LABEL_TAG+tag]; [self addSubview:readNewsLable];