我有一个包含多个单词的属性字符串。我正在尝试在 iOS7 上使用 Core Text 沿着 CGPathRef 绘制它。但是,即使我将 CTLineBreakMode 设置为 kCTLineBreakByWordWrapping,文本也不会在单词上换行,而是在字符上换行。例如,红色、绿色、蓝色和紫色的饼图都有足够的空间来使用自动换行来显示字符串,但 Core Text 坚持在这些情况下对字符进行换行。我究竟做错了什么?
UIFont *font = [UIFont boldSystemFontOfSize:10];
CGContextSetLineWidth(context, 1.0);
CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
CGMutablePathRef pathSmaller = CGPathCreateMutable();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGPathMoveToPoint(pathSmaller, NULL, self.center.x, self.center.y);
CGPathAddArc(pathSmaller, NULL, self.center.x, self.center.y, radius - 5, (beginAngle - 90 - 1) * M_PI / 180, (endAngle - 90 - 1) * M_PI / 180, NO);
CGPathCloseSubpath(pathSmaller);
CGContextAddPath(context, pathSmaller);
CGContextFillPath(context);
CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping;
CTParagraphStyleSetting settings[] = {
{ kCTParagraphStyleSpecifierLineBreakMode, sizeof(CTLineBreakMode), &lineBreakMode },
};
CTParagraphStyleRef pstyle = CTParagraphStyleCreate(settings, 1);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"Mortgage Insurance"];
CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attrString, CFRangeMake(0, [attrString length]), kCTParagraphStyleAttributeName, pstyle);
CFRelease(pstyle);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
CTFrameRef theFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [attrString length]), pathSmaller, NULL);
CFRelease(framesetter);
CTFrameDraw(theFrame, context);
CFRelease(theFrame);