1

I am trying to achieve word wrapping with hyphenation but do not want to see the hyphenation character in the uitextview.

In viewDidLoad;

textFieldParagraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping
textFieldParagraphStyle.alignment = NSTextAlignment.Center
textFieldParagraphStyle.hyphenationFactor = 1.0

Everything works well but is it possible to get rid of hyphenation character with preserving the correct syllable wrapping?

To illustrate, suppose we have the word understand. If I use word wrapping with hypenation factor set 1.0, the uitextview divided the word like;

under-
stand

What I want to achieve is;

under
stand

Sulthan
  • 128,090
  • 22
  • 218
  • 270

1 Answers1

0

Here is a code snippet. Read about CFStringGetHyphenationLocationBeforeIndex here to understand how the method works. Hope it helps. If your text view width allows to have multiple words in a line, then probably you should define at what syllable in the word you should make hyphenation, you can change location and limitRange to get other hyphens, not only in the word's "middle", like in my example.

- (NSString*)hyphenatedWordFromWordString:(NSString*)word
{
    CFStringRef strRef = (__bridge CFStringRef)word;
    CFIndex location = _textView.text.length;
    CFRange limitRange = CFRangeMake(0, location);
    CFOptionFlags flags = 0;
    CFLocaleRef locale = (__bridge CFLocaleRef)[NSLocale currentLocale];

    CFIndex index = CFStringGetHyphenationLocationBeforeIndex(strRef, location, limitRange, flags, locale, NULL);
    if (index != kCFNotFound) {
        word = [NSString stringWithFormat:@"%@\n%@", [word substringToIndex:index], [word substringFromIndex:index]];
    }

    return word;
}
schmidt9
  • 4,436
  • 1
  • 26
  • 32
  • Could I use this function to force line break in every line finish in a multiple line text and word wrapping existence? – memetcircus Mar 14 '16 at 12:22
  • The real problem is; suppose we have a string "Hello Alice. Do you understand?" and two lines of textview. The first line finishes at the middle of understand. In the current situation textview fully puts understand in the next line. I want to divide it like "under" in the first line, "stand" in the second line without hyphens. – memetcircus Mar 14 '16 at 12:32