具有“ – ”字符的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
- CGContextShowTextAtPoint和空格
- Swift 4 CTRubyAnnotation不起作用
- 无法将types'CFString'的值转换为期望的参数types'UnsafePointer <Void>'(又名'UnsafePointer <()>')
- 在iOS中使用Core Textalignment多个大小的文本垂直中心而不是基线
- CoreText是否支持Small Caps?
- 检测轻拍的字符。 UITextView characterRangeAtPoint总是返回nil
- NSAttributedString – 获取字体属性
- 获取UILabel中的每一行文本
- 核心文本与iOS上的不对称形状