NSTextView with long NSMutableAttributedText low scrolling and loading speed

帅比萌擦擦* 提交于 2019-12-24 00:39:55

问题


I have a uitextview that most of times should show long attributedtexts. I use below code for creating my attributed text:

 //Add each line of doa to an element of array for future usage
    NSArray *paragraphs = [self.Text componentsSeparatedByString:@"\r\n"];

    MixedContent= [[NSMutableAttributedString alloc]init];
    ArabicContent= [[NSMutableAttributedString alloc]init];
    TranslatedContent= [[NSMutableAttributedString alloc]init];

    NSString * temp=@"";

    for (int i=0; i< paragraphs.count; i++) {

        if([paragraphs[i] hasPrefix:@"$2."]){

            temp=  [paragraphs[i] stringByReplacingOccurrencesOfString:@"$2." withString:@"" options:1 range:NSMakeRange(0, 3)];

            NSMutableAttributedString* tempMutable=  [[NSMutableAttributedString alloc] initWithString:temp attributes:doaDescriptionAttributes];

            //Go to next line
            [tempMutable  appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@"\n\n"]];


            [ArabicContent appendAttributedString: tempMutable];

            [TranslatedContent appendAttributedString:tempMutable];

            [MixedContent appendAttributedString:tempMutable];


        }else if ([paragraphs[i] rangeOfString:@"$3."].location != NSNotFound){

            temp=  [paragraphs[i] stringByReplacingOccurrencesOfString:@"$3." withString:@"" options:1 range:NSMakeRange(0, 3)];

            NSMutableAttributedString* tempMutable=  [[NSMutableAttributedString alloc] initWithString:temp attributes:arabicDoaAttributes];

            [MixedContent appendAttributedString:tempMutable];
            [MixedContent  appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@"\n"]];

            [ArabicContent appendAttributedString:tempMutable];
            [ArabicContent appendAttributedString: [[NSMutableAttributedString alloc] initWithString:@" "]];

        }else if ([paragraphs[i] rangeOfString:@"$4."].location != NSNotFound){

            temp=  [paragraphs[i] stringByReplacingOccurrencesOfString:@"$4." withString:@"" options:1 range:NSMakeRange(0, 3)];

            NSMutableAttributedString* tempMutable=  [[NSMutableAttributedString alloc] initWithString:temp attributes:arabicDoaAttributes];

            [MixedContent appendAttributedString:tempMutable];
            [MixedContent  appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@"\n"]];

            [TranslatedContent appendAttributedString:tempMutable];
            [TranslatedContent appendAttributedString: [[NSMutableAttributedString alloc] initWithString:@" "]];
        }

        temp=nil;
    }

}

My text is contain 3 kind of text. Some description. Some Arabic text and their translation. Each of this part is showing with a specific color.

And I use this code for setting the text:

self.myTextView.attributedText= myAttributedText;

the problem is that when I set or change the text of this textview it takes about 2 seconds that app set or update the text. The waiting time is not good for my users.

Also when I scroll the textview it is not good at all. It scroll some paragraphs and then stop for and then scroll, stop and scroll,....

Actually it is slow on scrolling.

Is there any better ways for setting the text so that I have a better performance?

UPDATE

I ma thinking whethere it is possible to load the text by scrolling. Some thing like a lazy loader list but for uitextview. Is it possible?

UPDATE

When I add the text without any attributes. (I mean all lines with same color) The scroll works fine.

UPDATE

It is about the amount of texts. When I reduce the amount of texts. For example instead of setting 10000 character I set 500 character and now it is pretty fast. How can I fix it?

来源:https://stackoverflow.com/questions/24355793/nstextview-with-long-nsmutableattributedtext-low-scrolling-and-loading-speed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!