0

これら2つの違いにdrawこれらtextを入れPDFたとき、私はそれを理解できません。truncatespecific 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));

2 つ目:

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

4

1 に答える 1

0

1つの解決策は次のとおりです。fromnoを取得し、110であるupperCase lettersから削除されるカウントの35%を掛けます。actual stringproportionallyoriginal index

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

countからのupperCase手紙の取得string:

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

編集:これらの正確な解決策が見つかりません.......

于 2013-07-11T05:03:47.537 に答える