UITableviewCell中的核心文本重叠并重复并叠加在其他单元格上

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

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

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

 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类参考文档意味着“所有连字”。 请注意,这是我正在测试的东西,我还没有看到它的效果,但我知道连字是在某些平台上渲染一些阿拉伯字体的常见问题。

关于这个问题,你可能面临的另一个常见问题是阿拉伯语文本的行间距和多行文本的轻微重叠,我发现当与NSParagraphStyle一起使用时, NSAttributedString也可以是一个很好的解决方案(Hooray再次对于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上添加自定义字体时,添加有关常见错误的有用post, 以便在iOS上添加自定义字体时作为“清单”参考。

实际上通过在cellforRowAtIndexPath中添加以下行来自行解决问题:

  if (cell == nil) { cell = [[QuranVersesViewCell alloc] init]; ..... 

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