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

1

在我在这里NSAttributedStrings阅读本教程(在 iOS 6 中提供)之前,我遇到了同样的问题。

以下代码将解决您的问题:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:info.text attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Scheherazade" size:32], NSLigatureAttributeName: @2}];

cell.textLabel.attributedText = attributedString;

出于好奇,我是否正确地说您选择使用CoreText是因为难以渲染嵌入的阿拉伯字体?我冒险猜测是因为当我遇到我目前正在开发的古兰经应用程序的确切问题时,我试图使用与您在代码中所做的类似的方法。如果是这样,那么我可以确认使用NSAttributedString也可以解决问题。如果您在上面的代码中注意到,我还根据官方 Apple 类参考文档将其设置为“所有连字” NSLigatureAttributeName2请注意,这是我目前正在测试的东西,我还没有看到它的效果,但我知道连字是在某些平台上渲染某些阿拉伯字体时的常见问题。

在这个主题上,您可能面临的另一个常见问题是阿拉伯文本的行间距和多行文本的轻微重叠,我发现NSAttributedStringNSParagraphStyle(Hooray again for NSAttributedString! )。只需将上面的代码修改如下:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:info.text attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Scheherazade" size:32], NSLigatureAttributeName: @2}];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:20];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [info.text length])];

cell.textLabel.attributedText = attributedString;

希望这可以帮助您或其他任何人!


编辑 - 在iOS 应用程序中添加自定义字体的常见错误中添加这篇有用的帖子,作为在 iOS 上添加自定义字体时的“清单”参考。

于 2013-06-25T23:25:07.650 回答
0

实际上通过在 cellforRowAtIndexPath 中添加以下行自己解决了这个问题:

 if (cell == nil)
            {

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

并且仅在单元格为零时才进行所有初始化和设置。最重要的是标记视图层并仅为匹配的标记视图设置文本......

于 2013-06-03T23:32:34.140 回答