How to enable hyphenation in UITextView with non-English text?
Asked Answered
P

1

10

I use paragraphStyle.hyphenationFactor = 1, but it works fine only for English text. How to enable hyphenation in UITextView with non-English text or set locale to UITextView?

Peavy answered 4/4, 2014 at 8:45 Comment(0)
P
4

With this and this I found solution:

NSError *error = nil;
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"LOCALE"];
NSString *hyphenatedString = [string softHyphenatedStringWithLocale:locale error:&error];

NSString *htmlString = [NSString stringWithFormat:@"<html> \n"
                        "<head> \n"
                        "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> \n"
                        "<style type=\"text/css\"> \n"
                        "html {-webkit-hyphens: auto; } \n"
                        "</style> \n"
                        "</head> \n"
                        "<body>%@</body> \n"
                        "</html>", hyphenatedString];


@try {
    SEL setContentToHTMLString = NSSelectorFromString([@[@"set", @"Content", @"To", @"HTML", @"String", @":"] componentsJoinedByString:@""]);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [textView performSelector:setContentToHTMLString withObject:htmlString];
#pragma clang diagnostic pop
}
@catch (NSException *exception)
{
    textView.text = hyphenatedString;
}
Peavy answered 12/4, 2014 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.