0

Core Text を使用して UITableviewCell のコンテンツにテキストを追加していますが、スクロールして他のセルに重ねると、アラビア語のコンテンツが重複して繰り返されているようです。

また、ページ上で他の要素も使用していますが、それらは問題なく表示され、繰り返されていません。コアテキストだけが繰り返されているようです。

理由がわかりません。

これが私のコードです:

     - (CTFontRef)newCustomFontWithName:(NSString *)aFontName
                            ofType:(NSString *)type
                        attributes:(NSDictionary *)attributes {
    NSString *fontPath = [[NSBundle mainBundle] pathForResource:aFontName ofType:type];

    NSData *data = [[NSData alloc] initWithContentsOfFile:fontPath];
    CGDataProviderRef fontProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);


    CGFontRef cgFont = CGFontCreateWithDataProvider(fontProvider);
    CGDataProviderRelease(fontProvider);

    CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attributes);
    CTFontRef font = CTFontCreateWithGraphicsFont(cgFont, 0, NULL, fontDescriptor);
    CFRelease(fontDescriptor);
    CGFontRelease(cgFont);
    return font;
}

- (CATextLayer *)customCATextLayer:(NSString *)textString {
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithFloat:24.f], (NSString *)kCTFontSizeAttribute,
                                [NSNumber numberWithInt:1], (NSString *)kCTLigatureAttributeName,
                                nil];

    CTFontRef font = [self newCustomFontWithName:@"me_quranKer6"
                                          ofType:@"ttf"
                                      attributes:attributes];

    CATextLayer *normalTextLayer = [[CATextLayer alloc] init];
    normalTextLayer.font = font;
    normalTextLayer.string = textString;
    normalTextLayer.wrapped = YES;
    normalTextLayer.foregroundColor = [[UIColor blackColor] CGColor];
    normalTextLayer.fontSize = 24.f;
    normalTextLayer.alignmentMode = kCAAlignmentCenter;
    normalTextLayer.frame = CGRectMake(0.f, 10.f, 320.f, 32.f);

    CFRelease(font);
    return normalTextLayer;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QuranVersesViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"verseCell"];

    Verse *verse = [self.fetchedResultsController objectAtIndexPath:indexPath];

     //English Content starts


    NSMutableAttributedString * englishAttributedString;
    if (!englishAttributedString)
        englishAttributedString = [[NSMutableAttributedString alloc] initWithString:@""];

    NSMutableAttributedString * englishSubtitleAttributedString;


    NSMutableAttributedString * englishVerseAttributedString;
    if (!englishVerseAttributedString)
        englishVerseAttributedString = [[NSMutableAttributedString alloc] initWithString:verse.english_version];

    NSMutableAttributedString * englishFootnoteAttributedString;
    if (!englishFootnoteAttributedString)
        englishFootnoteAttributedString = [[NSMutableAttributedString alloc] init];



    NSString *englishString = @"";

    if(verse.subtitle.length>0)
    {
        NSMutableParagraphStyle *mutParaStyle=[[NSMutableParagraphStyle alloc] init];

        [mutParaStyle setAlignment:NSTextAlignmentCenter];




        englishSubtitleAttributedString = [[NSMutableAttributedString alloc] initWithString:verse.subtitle];

    [englishSubtitleAttributedString addAttributes:[NSDictionary dictionaryWithObject:mutParaStyle
                                                       forKey:NSParagraphStyleAttributeName]
                     range:NSMakeRange(0,[[englishSubtitleAttributedString string] length])];
        [englishAttributedString appendAttributedString:englishSubtitleAttributedString];

        [englishAttributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:30] range:NSRangeFromString(verse.subtitle)];
        NSLog(@"text us %@", englishAttributedString);

    }// englishString = [englishString stringByAppendingString:[NSString stringWithFormat:@"%@\n\n", verse.subtitle]];

    [englishAttributedString appendAttributedString:englishVerseAttributedString];

    englishString = [englishString stringByAppendingString:[NSString stringWithFormat:@"[%@:%@] %@\n",  verse.whichSura.sura_no, verse.verse_no, verse.english_version]];

    if(verse.footnote.length>0)
        englishString = [englishString stringByAppendingString: [NSString stringWithFormat:@"\n%@\n", verse.footnote]];


englishString =  [englishString stringByReplacingOccurrencesOfString:@"“" withString:@"\"" ];
  englishString =   [englishString stringByReplacingOccurrencesOfString:@"_" withString:@"\n" ];


  cell.quranVerseEnglishTextView.attributedText = englishAttributedString;
  [cell.quranVerseEnglishTextView autoResizeWithMaxWidth:MAX_TEXT_WIDTH];


    cell.quranVerseEnglishTextView.backgroundColor = [UIColor clearColor];
  //English Content starts


//Arabic Content

    CATextLayer *arabicTextLayer = [self customCATextLayer:verse.arabic_version];
    [cell.arabicView.layer addSublayer:arabicTextLayer];







    return cell;

}
4

2 に答える 2

0

cellforRowAtIndexPath に次の行を追加して、実際に問題を自分で修正しました。

 if (cell == nil)
            {

                cell = [[QuranVersesViewCell alloc] init];
              .....

また、セルがnilの場合にのみ、すべての初期化と設定を行いました。そして最も重要なのは、ビューレイヤーにタグを付け、一致するタグ付きビューのみにテキストを設定することです...

于 2013-06-03T23:32:34.140 に答える