我正在尝试在iOS App中绘制文字。在绘制字符串'A-BCDEFGHIJKLM'时,它会被修剪为'A - '。
以下是我的代码:
NSString *textToDraw=@"A-BCDEFGHIJKLM";
CGRect frameRect=CGRectMake(10, 10, 80, 15);
float fontSize=9;
UIColor *textColor=[UIColor blackColor];
BOOL isBold=YES;
CTTextAlignment alignMent=kCTTextAlignmentLeft;
CFStringRef stringRef = (CFStringRef)textToDraw;
// CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
NSMutableAttributedString* attString = [NSMutableAttributedString attributedStringWithString:textToDraw];
NSInteger _stringLength=[textToDraw length];
CTTextAlignment theAlignment = alignMent;
CTParagraphStyleSetting theSettings[1] =
{
{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),&theAlignment
}
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, 1);
[attString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)paragraphStyle range:NSMakeRange(0, _stringLength)];
CFAttributedStringRef attrString =(CFAttributedStringRef) attString;
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
CFRelease(frameRef);
CFRelease(stringRef);
CFRelease(framesetter);
此处' CTFramesetterCreateFrame '正在将字符串' A-BCDEFGHIJKLM '修剪为' A - '。
当我打印出“ frameRef ”的描述时,它看起来如下所示(请参阅 CTRun中的“ string ='A - '”部分):
Printing description of frameRef:
<CTFrame: 0x162098b0>{visible string range = (0, 2), path = <CGPath 0x16209b40>, attributes = (null), lines = <CFArray 0x16209c00 [0x3102e3b0]>{type = mutable-small, count = 1, values = (
0 : <CTLine: 0x16209c20>{run count = 1, string range = (0, 2), width = 12, A/D/L = 9.24023/2.75977/0, glyph count = 2, runs = (
<CTRun: 0x16209cc0>{string range = (0, 2), string = "A-", attributes = <CFBasicHash 0x16209b10 [0x3102e3b0]>{type = mutable dict, count = 2,
entries =>
0 : <CFString 0x31114b08 [0x3102e3b0]>{contents = "NSFont"} = <CTFont: 0x14e43990>{name = Helvetica, size = 12.000000, matrix = 0x0, descriptor = <CTFontDescriptor: 0x14e43ce0>{attributes = <CFBasicHash 0x14e43d10 [0x3102e3b0]>{type = mutable dict, count = 1,
entries =>
1 : <CFString 0x31116ea8 [0x3102e3b0]>{contents = "NSFontNameAttribute"} = <CFString 0x31112408 [0x3102e3b0]>{contents = "Helvetica"}
}
>}}
2 : <CFString 0x32edd1d8 [0x3102e3b0]>{contents = "NSParagraphStyle"} = <CTParagraphStyle: 0x14f00c90>{base writing direction = -1, alignment = 0, line break mode = 0, default tab interval = 0
first line head indent = 0, head indent = 0, tail indent = 0
line height multiple = 0, maximum line height = 0, minimum line height = 0
line spacing adjustment = 0, paragraph spacing = 0, paragraph spacing before = 0
tabs = <CFArray 0x14f00a80 [0x3102e3b0]>{type = immutable, count = 12, values = (
0 : <CTTextTab: 0x14f00cc0>{location = 28, alignment = 0, options = (null)}
1 : <CTTextTab: 0x14f00cf0>{location = 56, alignment = 0, options = (null)}
2 : <CTTextTab: 0x14f00dc0>{location = 84, alignment = 0, options = (null)}
3 : <CTTextTab: 0x14f00df0>{location = 112, alignment = 0, options = (null)}
4 : <CTTextTab: 0x14f00e20>{location = 140, alignment = 0, options = (null)}
5 : <CTTextTab: 0x14f00e50>{location = 168, alignment = 0, options = (null)}
6 : <CTTextTab: 0x14f00e80>{location = 196, alignment = 0, options = (null)}
7 : <CTTextTab: 0x14f00eb0>{location = 224, alignment = 0, options = (null)}
8 : <CTTextTab: 0x14f009c0>{location = 252, alignment = 0, options = (null)}
9 : <CTTextTab: 0x14f009f0>{location = 280, alignment = 0, options = (null)}
10 : <CTTextTab: 0x14f00a20>{location = 308, alignment = 0, options = (null)}
11 : <CTTextTab: 0x14f00a50>{location = 336, alignment = 0, options = (null)}
)}}
}
}
)
}
)}}
它适用于其他正常字符串,但当我通过'A-BCDEFGHIJKLM'时,它会被修剪。
有没有解决方案?
提前致谢。