在iOS的drawrect方法中,带有'-'字符的字符串无法正确绘制 [英] A string with '-' character is not drawing properly in drawrect method in iOS

查看:128
本文介绍了在iOS的drawrect方法中,带有'-'字符的字符串无法正确绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS中的coretext。

I'm working on coretext in iOS.

我通过替换<$实现了自定义 UIView c $ c> -drawRect 方法,其代码如下:

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

在这里,当我将输入字符串设置为' ABCDEFGHIJKLMNOPQRST >,则将我需要的输出显示为 ABCDEFGHIJ ,并根据宽度 80 将其截断。

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'.

但是问题是当我将输入字符串传递为' A-BCDEFGHIJ'时,它给我的输出是' A-'。

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

至少应根据宽度打印 A-BCDEFGHI 。但这只是打印 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?

下面是屏幕截图:

字符串'ABCDEFGHIJKLMNOPQRST'的输出:

字符串'A-BCDEFGHIJ'的输出:

请帮助我。

预先感谢。

推荐答案

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

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

考虑使用Unicode不间断连字符(U + 2011)

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

例如

NSString *textToDraw = @"A\u2011BCDEFGHIJ";

查看更多: Unicode换行算法

这篇关于在iOS的drawrect方法中,带有'-'字符的字符串无法正确绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆