Search code examples
iosuilabeltelegramdirection

How to show custom line direction inside UIlabel of UITableViewCell with better performance


I want to show some text inside UIlabel of a UITableViewCell for each cell. Inside the text, for each line of a text,line direction should displayed correctly. So if the line language is arabic it should be RTL and if it's english it should be LTR. I don't want detect line seperator in cellForRow delegate and then build NSAttributedString because detection in cellForRow has low performance and I have a huge table with huge texts. How can solve this scenario with good performance?
I also don't know how Telegram solve this problem, if anyone know tell me please. Any help appreciated.
Edit: Each text that inserted inside UILabel can have different lines that each line can has different language. So for each line I need different direction. Inside UILabel or UITextView I just can set direction for total text not each line of text.


Solution

  • For performance reasons, it is always advisable to pre-process your data into NSAttributedStrings, for RTL (line by line) - and put it into your table-backing array, before you call reloadData().

    cellForRow should process minimal data. cellForRow should only be used to choose which data to show, depending on the logic. Such as picking it from Array 1 or Array 2 depending upon some UI state that user is in, such as searching mode / collapse-expand mode / language 1-language 2 data etc.

    This data itself should be pre-processed as much as possible, and must be rendered readily. As soon as you are done processing, call reloadData to render it as-is.