如何在不显示连字符的情况下包装文本(使用相同的单词拆分规则)?

时间:2016-03-13 19:28:18

标签: ios uitextview

我正在尝试使用连字符进行自动换行,但不希望在uitextview中看到连字符。

在viewDidLoad;

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

一切运作良好,但是可以通过保留正确的音节包装来摆脱连字符吗?

为了说明,假设我们有“理解”这个词。如果我使用带有夸张因子集1.0的自动换行,则uitextview将单词分为;

  

不足
  立

我想要实现的是;

  


  立

1 个答案:

答案 0 :(得分:0)

这是一段代码段。阅读CFStringGetHyphenationLocationBeforeIndex here,了解该方法的工作原理。希望能帮助到你。如果您的文本视图宽度允许在一行中包含多个单词,那么您可能应该定义单词中应该进行连字符的音节,您可以更改locationlimitRange以获取其他连字符,而不是只在单词的“中间”,就像在我的例子中一样。

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