如何在UITextView中使用非英文文本启用连字?

时间:2014-04-04 08:45:04

标签: ios objective-c uitextview hyphenation

我使用paragraphStyle.hyphenationFactor = 1,但它仅适用于英文文本。 如何在UITextView中使用非英语文本启用连字符或将语言环境设置为UITextView

1 个答案:

答案 0 :(得分:4)

使用thisthis我找到了解决方案:

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;
}