ios - UITableviewCell 内容中的核心文本重叠、重复并叠加在其他单元格上

标签 ios uitableview core-graphics core-text

我正在使用 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;

}

最佳答案

我遇到了同样的问题,直到我在本教程 here 中阅读了有关 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 也可以解决问题。如果您在上面的代码中注意到,我还将 NSLigatureAttributeName 设置为 2,根据官方 Apple 类引用文档,这意味着“所有连字”。请注意,这是我目前正在测试的东西,我还没有看到其效果,但我知道连字是某些平台上某些阿拉伯字体渲染的常见问题。

在这个主题上,您可能面临的另一个常见问题是阿拉伯文本的行距和多行文本的轻微重叠,我发现 NSAttributedString 也可以是与NSParagraphStyle一起使用是一个很好的解决方案(再次为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;

希望这对您或其他人有帮助!


编辑 - 在 Common Mistakes With Adding Custom Fonts to Your iOS App 上添加这篇有用的帖子在 iOS 上添加自定义字体时作为“ list ”引用。

关于ios - UITableviewCell 内容中的核心文本重叠、重复并叠加在其他单元格上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16754488/

相关文章:

ios - 当手指经过时,Swift Button 的作用就像一个传感器

javascript - iOS 中使用 javascript 预加载图像

swift text.draw 方法搞乱了 CGContext

ios - 快速合并两个图像

objective-c - 当我设置 anchor 时,我的图像没有加载

iphone - 来自 iphone 应用程序的定位服务

iphone - 如何更改单元格的 textLabel 框架?

ios - 如何动态调整TableView Cell Height和TableView Height的大小

ios - 在 UITableView 处于编辑模式时选择行

ios - 从 View Controller 传递的数据返回 null