Lines missing from tall UILabel when embedding NSTextAttachment

前端 未结 4 1948
陌清茗
陌清茗 2020-12-16 12:12

I can create a multi-line NSAttributedString by using escaped new-line characters (@\"\\n\"). With iOS 7, I can now embed a UIImage in

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 12:52

    It really has nothing to do with the NSTextAttachment. It's that in iOS 7 as released so far, UILabel is not very good at drawing attributed strings. A simple attributed string with some underlining and a centered paragraph style will show up blank, or partially blank, in a UILabel; the same attributed string draws fine in a UITextView.

    So, one solution for now is: Use UITextView instead. This is actually a pretty good solution because, in iOS 7, UITextView is just a wrapper around the Text Kit stack. So it is drawing the attributed string in a straightforward way. It is not hampered by the under-the-hood relationship to Web Kit that it had in previous iOS versions.

    On the other hand, I have also found a workaround for this UILabel bug; you have to fiddle with the number of lines of the label and the string in such a way as to jam the text up to the top of the label: see my answer here - https://stackoverflow.com/a/19409962/341994

    Or you could just wait for Apple to fix the bug and keep your fingers crossed. EDIT: In iOS 7.1, it appears that the bug will be fixed and no workaround will be needed.

提交回复
热议问题