【问题标题】:Difference in these two CGContextShowTextAtPoint?这两个CGContextShowTextAtPoint的区别?
【发布时间】:2013-07-10 12:24:52
【问题描述】:

当我 draw 这些 textPDFtruncatespecific width 这两个区别时,我不明白。

第一个

NSString *strText12 = @"HASDHADH  skjdfhhs HKSJDHF.;; []'.hfkjhsfS SDHJHFSH jsfsjdfsn eiwj NJSDSJDF SDLKJJ sfkjsj wreoiwuowu 87243 7298 72jsdj h@#$$$!@$$";

if (strText12.length > 110 ) {
    strText12 = [strText12 substringToIndex:110];
    strText12 = [strText12 stringByAppendingFormat:@"...."];
}

CGContextSelectFont (aCgPDFContextRef, "Helvetica", 12, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (aCgPDFContextRef, kCGTextFill);
CGContextSetRGBFillColor (aCgPDFContextRef, 0, 0, 0, 1);
const char *text1 = [strText12 UTF8String];
CGContextShowTextAtPoint (aCgPDFContextRef, 10,50.0, text1, strlen(text1));

第二个

NSString *strText = @"hasdhadh  skjdfhhs hksjdhf.;; []'.hfkjhsfs sdhjhfsh jsfsjdfsn eiwj njsdsjdf sdlkjj sfkjsj wreoiwuowu 87243 7298 72jsdj h@#$$$!@$$";

if (strText.length > 110 ) {
    strText = [strText substringToIndex:110];
    strText = [strText stringByAppendingFormat:@"...."];
}

CGContextSelectFont (aCgPDFContextRef, "Helvetica", 12, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (aCgPDFContextRef, kCGTextFill);
CGContextSetRGBFillColor (aCgPDFContextRef, 0, 0, 0, 1);
const char *text = [strText UTF8String];
CGContextShowTextAtPoint (aCgPDFContextRef, 10,10.0, text, strlen(text));

PDF 格式

编辑:即使使用CoreText result 也是same

【问题讨论】:

  • 我不明白这个问题。您将两个字符串都截断为 110 个字符。大写字母“HASDHADH”比小写字母“hasdhadh”,因此第一个字符串占用更多空间。
  • 如何能够将字符串截断到特定宽度,取决于字符串可能在 osx 中的任何大小写字母?

标签: objective-c macos cocoa pdf


【解决方案1】:

一种解决方案可以是:从actual string 获取upperCase letters 中的no,现在将proportionally 乘以将从original index 中删除的计数的35%,即110。

NSString *textToDraw = some string
int range = 100;
NSString *strTruncate = textToDraw;
if (strTruncate.length > range ) {
    strTruncate = [strTruncate substringToIndex:range];

    //NSLog(@"now : %@",strTruncate);

    int actualIndex = 0;
    int count = [self getNoUpperCaseCharWithString:strTruncate];
    if (count>0) {
        float minusIndex = (float)count*0.35;
        actualIndex = range - (int)minusIndex;
        //actualIndex -= 4;
    }
    else{
        actualIndex = range;
    }

    if (strTruncate.length > actualIndex ) {
        textToDraw = [strTruncate substringToIndex:actualIndex];
        textToDraw = [strTruncate stringByAppendingFormat:@"..."];
    }

    //NSLog(@"after : %@",textToDraw);
}

string 获取upperCasecount 信件:

-(int)getNoUpperCaseCharWithString:(NSString *)string
{
  int count=0;
  for (int i = 0; i < [string length]; i++) {
    BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[string characterAtIndex:i]];
    if (isUppercase == YES)
        count++;
  }
  return count;
}

编辑:没有找到准确的解决方案......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 2018-06-12
    • 2020-12-25
    • 1970-01-01
    相关资源
    最近更新 更多