A string with '-' character is not drawing properly in drawrect method in iOS

两盒软妹~` 提交于 2020-01-03 06:04:56

问题


I'm working on coretext in iOS.

I have implemented a custom UIView by replacing -drawRect method with the following code:

- (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);
}

Here when i put the input string as 'ABCDEFGHIJKLMNOPQRST', it's giving my required output as 'ABCDEFGHIJ' which is truncated as per the width '80'.

But the problem is when i pass input string as 'A-BCDEFGHIJ', it's giving me output as 'A-'.

At least 'A-BCDEFGHI' should be printed as per the width. But it's only printing 'A-'. It's not truncating properly.

Am i missing something in my code?

Below are the screenshots:

Output for the string 'ABCDEFGHIJKLMNOPQRST':

Output for the string 'A-BCDEFGHIJ':

Please help me.

Thanks in Advance.


回答1:


-, or "hyphen" is a breaking character, which starts a new line (similar to \n).

Consider using Unicode NON-BREAKING HYPHEN (U+2011)

e.g.

NSString *textToDraw = @"A\u2011BCDEFGHIJ";

See more: Unicode Line Breaking Algorithm



来源:https://stackoverflow.com/questions/25077900/a-string-with-character-is-not-drawing-properly-in-drawrect-method-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!