More time is taken to display NSAttributed string with custom fonts
Asked Answered
P

1

0

I am displaying NSAttributed string column wise using Core Text. It is working fine. When using system font, it is displayed without any delay both in simulator and device. But when using custom font, more time is taken to display the content in device. But in the simulator, the result is quick.

- (void)updateAttributedString
{
        // Existing Code
    if (self.text != nil)
    {

        self.attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
        NSRange range = NSMakeRange(0, [self.text length]);
         // Regarding Fixed font
//        [ self.attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"TAUN_Elango_Abirami" size:20] range:range];//This is my custom font


           // Regarding custom Font using below code
        if (self.font != nil) {
            CTFontRef font = [self createCTFont];
            [self.attributedString addAttribute:(NSString *)kCTFontAttributeName
                                          value:(__bridge id)font
                                          range:range];
            CFRelease(font);
        }
      }
}



- (CTFontRef)createCTFont;
{

    CTFontRef font = CTFontCreateWithName((CFStringRef)self.fontName, self.pointSize, NULL);

    return font;
}

If I add the following line of code,

 [self.attributedString addAttribute:(NSString *)kCTFontAttributeName
                                              value:(__bridge id)font
                                              range:range];

displaying the attributed string is slow in device. But, in simulator it is quick. If I don't add that piece of code, the text is displayed quickly in both simulator and device.

Plast answered 22/2, 2017 at 5:11 Comment(8)
What's your question?Leniency
@AlbertRenshaw I need to use custom font and I want the text to be displayed without delay in the device . Slowness is the issue.Plast
If you use Time Profiling of XCode Instruments, does it point out a specific line? Also, do you need to use CoreText, CFStuff and bridge?Whitewing
here only taking time [self.attributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:range];Plast
if i comment it, its working speed in both simulator and device.Plast
@matt that is also i did [ self.attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"TAUN_Elango_Abirami" size:20] range:range];Plast
still it is slowing, i commented, if you wish, i can share my sample. i am really need help on this? i am looking forward your helpPlast
its very slow in device only but simulator working excepted speed.Plast
D
0

Create your font object one time and hold onto it. I'd probably cache them in a static dictionary if you have multiple and need to share across objects. Don't create a new one every time you update the string. The font object is almost certainly doing all its complicated decoding work at the point that it's first needed (not the point that you create it). System fonts are always loaded and decoded, but custom fonts likely aren't kept around if nothing is referencing them.

You may also want to experiment with UIFont rather than CTFont here. UIFont is a higher-level object, and my experience with it is that it caches more. I haven't explored this particular situation. In general, you should generally use UIKit types unless you really need Core Text. This can be counter-intuitive, since "isn't lower level faster?" Well, lower level can be faster, if you know exactly what you're doing. But really lower level just means "you have to take care of more stuff yourself." Trusting UIKit is usually the better first-order solution until you know you need something more fine-grained.

It is not surprising that the simulator would be faster. That's running on a Mac which has dramatically more processing power and a much faster disk than an iPhone. When you run things on the simulator, they're actually just Mac apps that run in a special UI; it's not a full emulator like Android uses.

Destalinization answered 2/3, 2017 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.