NSLayoutManager boundingRect(forGlyphRange:in:) returns wrong value for RTL languages

只谈情不闲聊 提交于 2021-02-10 23:46:10

问题


In my app, I draw custom background under lines of text in UITextView. To do so, I use boundingRect(forGlyphRange:in:) method of NSLayoutManager. It works well for LTR languages (Latin, Cyrillic, Chinese etc): But if I use it for RTL texts (Hebrew, Farsi etc), the method returns wrong bounding rect (see padding on the left edge):

These additional paddings seem to have some logic. The first character determines the size of the padding for a line:

What the reason for this extra padding and how to get the correct bounding rect?

Here is a demo: https://www.dropbox.com/s/bpsr7bhhd7gqm2z/hebrew.zip?dl=0

UPD: seems this function gives a wrong result for all alphabets that use, I don't know how it's called, mutating characters. Devanagari for example:


回答1:


Seems boundingRect(forGlyphRange:in:) counts exactly glyphs with their spaces for attaching neighboring characters but not the final composed piece of text which gives extra paddings.

I solved my issue but employing boundingRect function of NSString. Check the result: https://www.dropbox.com/s/qq5lmo8fsozjqml/hebrew%202.zip?dl=0



来源:https://stackoverflow.com/questions/54161101/nslayoutmanager-boundingrectforglyphrangein-returns-wrong-value-for-rtl-lang

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