Core Text in UITableviewCell's content overlapping and repeating and superimpose on the other cells

后端 未结 2 1059
天命终不由人
天命终不由人 2021-01-16 19:23

I am using Core Text to add text to UITableviewCell\'s content but arabic content seems to be overlapping and repeating itself as I scroll and superimpose on the other cells

相关标签:
2条回答
  • 2021-01-16 20:09

    I was facing the same problem until I read up about NSAttributedStrings (made available in iOS 6) on this tutorial here.

    The following code will solve your issue:

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

    Out of curiosity, would I be correct to say that you opted to use CoreText because of difficulties in rendering embedded arabic fonts? I ventured the guess because I was attempting to use a similar method as you have done in your code when faced with that exact problem for a Quran app that I'm currently developing. If this so then I can confirm that using NSAttributedString also solves the problem. If you notice in the code above I've also set the NSLigatureAttributeName to 2 which according to the official Apple Class Reference Documentation means 'all ligatures'. Just note that this is something that I'm currently testing and I have yet to see the effects of this but I know that ligatures is a common problem in the rendering of some arabic fonts on certain platforms.

    While on the subject, another common problem you may be facing is the line-spacing of arabic text and the slight overlapping of multi-line text and I've found that NSAttributedString can also be a good solution when used together with NSParagraphStyle (Hooray again for NSAttributedString!). Simply modify the above code as below:

    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;
    

    Hope this helps you or anyone else out there!


    EDIT - Adding this helpful post on Common Mistakes With Adding Custom Fonts to Your iOS App for reference as a "checklist" when adding custom fonts on iOS.

    0 讨论(0)
  • 2021-01-16 20:10

    Actually fixed the issue myself by adding the following line in cellforRowAtIndexPath:

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

    and also did all the initialization and setting only when the cell was nil. And MOST importantly tagged the view layer and set the text for only the matching tagged view...

    0 讨论(0)
提交回复
热议问题