NSAttributedString
- XCode 4.0 이후 , iPhone SDK 에 추가됨
- 현재, NSAttributedString 은 사용가능하나, 받아주는 UI모듈이 없음
-
TODO : 현재 shadow 관련부분 수정 필요.
-
- (void)drawRect:(CGRect)rect
-
{
-
// Drawing code
-
[super drawRect:rect];
-
-
if(self.attrText.length == 0) return;
-
-
CGContextRef context = UIGraphicsGetCurrentContext();
-
CGContextClearRect(context, rect);
-
-
[self.backgroundColorset];
-
CGContextFillRect(context, rect);
-
-
CGFloat width = CGRectGetWidth(self.bounds) - kJTextViewPaddingSize * 2;
-
CGSize textSize = [[self.attrTextstring] sizeWithFont:[UIFontfontWithName:@"Helvetica"size:12]
-
constrainedToSize:CGSizeMake(width, UINT_MAX)
-
lineBreakMode:UILineBreakModeWordWrap];
-
-
-
-
-
CGContextTranslateCTM(context, 0, textSize.height);
-
CGContextScaleCTM(context, 1.0, -1.0);
-
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, 1.0));
-
-
UIBezierPath* path = [UIBezierPathbezierPathWithRect:CGRectMake(kJTextViewPaddingSize, -(kJTextViewPaddingSize), width, textSize.height)];
-
-
//줄간격 3px
-
float lineSpacing = 3;
-
float lineHieght = 12;
-
-
NSRange range = NSMakeRange(0, self.attrText.length);
-
CTLineBreakMode lineBreakeMode = kCTLineBreakByWordWrapping;
-
CFIndex theNumberOfSettings = 3;
-
CTParagraphStyleSetting theSettings[3] =
-
{
-
{kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(lineHieght), &lineHieght},
-
{kCTParagraphStyleSpecifierLineSpacing, sizeof(lineSpacing), &lineSpacing},
-
{kCTParagraphStyleSpecifierLineBreakMode, sizeof(lineBreakeMode), &lineBreakeMode}
-
};
-
CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
-
[self.attrTextaddAttribute:(id)kCTParagraphStyleAttributeNamevalue:(id)theParagraphRef range:range];
-
-
CFRelease(theParagraphRef);
-
-
if(self.hasShadow == YES){
-
self.shadowText = [self.attrTextmutableCopy];
-
NSRange shadowRange = NSMakeRange(0, shadowText.length);
-
[self.shadowTextaddAttribute:(NSString*)kCTForegroundColorAttributeNamevalue:(id)shadowColor.CGColorrange:shadowRange];
-
CTFramesetterRef shadowFramesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)shadowText);
-
UIBezierPath* shadowPath = [UIBezierPathbezierPathWithRect:CGRectMake(kJTextViewPaddingSize + shadowOffset.width, -(kJTextViewPaddingSize) - shadowOffset.height, width, textSize.height)];
-
CTFrameRef shadowFrame = CTFramesetterCreateFrame(shadowFramesetter, CFRangeMake(0, 0), shadowPath.CGPath, NULL);
-
CTFrameDraw(shadowFrame, context);
-
[self.shadowText release];
-
}
-
-
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.attrText);
-
CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path.CGPath, NULL);
-
-
CTFrameDraw(textFrame, context);
-
UIGraphicsPushContext(context);
-
}
This article was written in springnote.





