具有“ – ”字符的string在iOS中的绘制方法中不能正确绘制

我正在iOS的coretext上工作。

我已经通过用下面的代码replace-drawRect方法实现了自定义的UIView

 - (void)drawRect:(CGRect)rect { [super drawRect:rect]; CGRect frameRect=CGRectMake(50, 200, 80, 15); // NSString *textToDraw=@"A-BCDEFGHIJKLM"; // NSString *textToDraw=@"abc"; // NSString *textToDraw=@"ABCDEFGHIJKLMNOPQRST"; NSString *textToDraw=@"A-BCDEFGHIJ"; // NSString *textToDraw=@"A-BCDEFGHI"; CFStringRef stringRef = (__bridge CFStringRef)textToDraw; NSMutableAttributedString* attString = [[NSMutableAttributedString alloc]initWithString:textToDraw]; NSInteger _stringLength=[textToDraw length]; CTTextAlignment theAlignment = kCTLeftTextAlignment; // kCTRightTextAlignment; //kCTCenterTextAlignment; CTParagraphStyleSetting theSettings[1] = { { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),&theAlignment } }; CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, 1); [attString addAttribute:(id)kCTParagraphStyleAttributeName value:(__bridge id)paragraphStyle range:NSMakeRange(0, _stringLength)]; CFAttributedStringRef attrString =(__bridge 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); } 

在这里,当我把input的string作为“ ABCDEFGHIJKLMNOPQRST ”,它给我所需的输出为“ ABCDEFGHIJ ”,它被截断为宽度“ 80 ”。

但问题是,当我传递inputstring为“ A-BCDEFGHIJ” ,它给我输出为“ A- ”。

至less应该按照宽度打印“ A-BCDEFGHI ”。 但它只是打印“ A- ”。 这不是正确截断。

我在代码中丢失了什么?

以下是截图:

string“ABCDEFGHIJKLMNOPQRST”的输出: 在这里输入图像说明

string“A-BCDEFGHIJ”的输出: 在这里输入图像说明

请帮帮我。

提前致谢。

-或“连字符”是一个分隔符,它开始一个新行(类似于\n )。

考虑使用Unicode NON-BREAKING HYPHEN(U + 2011)

例如

 NSString *textToDraw = @"A\u2011BCDEFGHIJ"; 

查看更多: Unicode分行algorithm