Author Topic: UILabel: ­ (soft-hyphen) hyphenation support  (Read 3165 times)

claus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
UILabel: ­ (soft-hyphen) hyphenation support
« on: September 12, 2014, 10:06:52 AM »
Hello,

for a current project which is featuring lots of text, the client would like to have the text hyphenated automatically. I am using a slightly modified version of NHyphenator to automatically add soft-hyphenation symbols (any of ­, [sh] or the soft hyphen UTF-8 symbol) to a string before inserting it into a UILabel.

What is am missing now is that a UILabel automatically hyphenates the text at the correct positions, i.e. where a complete word does not fit into a row, but a part of a word containing &shy symbols still fits into a row.

Here is an example:

Original Text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Hyphenated Text using [sh] as hyphenation symbol:
Lo[sh]rem ip[sh]sum do[sh]lor sit amet, con[sh]sec[sh]te[sh]tur adi[sh]pi[sh]scing elit, sed do eiusmod tem[sh]por incidi[sh]dunt ut la[sh]bore et do[sh]lore mag[sh]na ali[sh]qua.

What I need now is UILabel to understand [sh] symbols and to hyphenate the text at these positions where appropriate:
Lorem ipsum dolor sit amet, con-
sectetur adipiscing elit, sed do
eiusmod tempor incididunt ut la-
bore et dolore magna aliqua.


My questions:
  • Has anybody needed or implemented this feature?
  • Would it be possible to add this feature to UILabel?
  • If not, can you give me some hints where to look/change the code to do this?

I am happy for any hints on this. Thanks for developing NGUI!

Kind regards,
claus

lamiumthetanker

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UILabel: ­ (soft-hyphen) hyphenation support
« Reply #1 on: September 12, 2014, 10:13:25 AM »
If I wanted to do something like this I would first look in NGUI's UILabel Class and see how it currently handles text wrapping when clamping. What I would then do is to see if there was anyway to make my own script which inherits from UILabel and see if I could override the specific the method that handles the clamping.

Then in this method I would specify the delimiter pattern to be this '[sh]' that you keep using throughout your text and simply run something similar to:

  1. String.Replace("[sh]", "-\n");

Of course this is providing that UILabel recognises a new-line character and that you're not performing any Trimming of special characters after you execute this function.

Just my two cents ;) Happy Coding!