连字库不适用于iOS 5

时间:2011-11-20 18:45:52

标签: iphone ios sdk hyphenation

我刚尝试了Tupil的连字符库。

这里提到http://blog.tupil.com/adding-hyphenation-to-nsstring/

虽然它在iOS 4.3下完美运行,但我没有让它与iOS 5一起使用。

我可以使用其他任何框架吗?我听说过CoreText,但我不知道从哪里开始。

提前致谢 马丁

2 个答案:

答案 0 :(得分:1)

我意识到已经有几年了,但我发现有一个核心基础功能可以建议连字点:CFStringGetHyphenationLocationBeforeIndex。它只适用于几种语言,但看起来它可能对窄标签问题非常有帮助。

<强>更新

以下是一些示例代码。这是一个CLI程序,它显示了一个单词的连字符:

#include <Cocoa/Cocoa.h>


int main(int ac, char *av[])
{
    @autoreleasepool {

    if(ac < 2) {
        fprintf(stderr, "usage:  hyph  word\n");
        exit(1);
    }
    NSString *word = [NSString stringWithUTF8String: av[1]];
    unsigned char hyspots[word.length];
    memset(hyspots, 0, word.length);
    CFRange range = CFRangeMake(0, word.length);
    CFLocaleRef locale = CFLocaleCreate(NULL, CFSTR("en_US"));
    for(int i = 0; i < word.length; i++) {
        int x = CFStringGetHyphenationLocationBeforeIndex(
                    (CFStringRef) word, i, range,
                    0, locale, NULL);
        if(x >= 0 && x < word.length)
            hyspots[x] = 1;
    }
    for(int i = 0; i < word.length; i++) {
        if(hyspots[i]) putchar('-');
        printf("%s", [[word substringWithRange: NSMakeRange(i, 1)] UTF8String]);
    }
    putchar('\n');

    }

    exit(0);
}

以下是构建和运行它时的外观:

$ cc -o hyph hyph.m -framework Cocoa
$ hyph accessibility
ac-ces-si-bil-i-ty
$ hyph hypothesis
hy-poth-e-sis

这些连字符与OS X字典完全一致。我在iOS中使用它来解决一个狭窄的标签问题,它对我来说效果很好。

答案 1 :(得分:0)

我写了一个基于类别的Jeffrey的答案,为任何字符串添加“软连字符”。这些是“ - ”,在渲染时不可见,而只是排队等待CoreText或UITextKit知道如何分解单词。

NSString *string = @"accessibility tests and frameworks checking";
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
NSString *hyphenatedString = [string softHyphenatedStringWithLocale:locale error:nil];
NSLog(@"%@", hyphenatedString);

输出ac-ces-si-bil-i-ty tests and frame-works check-ing


的NSString + SoftHyphenation.h

typedef enum {
    NSStringSoftHyphenationErrorNotAvailableForLocale
} NSStringSoftHyphenationError;

extern NSString * const NSStringSoftHyphenationErrorDomain;
extern NSString * const NSStringSoftHyphenationToken;

@interface NSString (SoftHyphenation)

- (BOOL)canSoftHyphenateStringWithLocale:(NSLocale *)locale;
- (NSString *)softHyphenatedStringWithLocale:(NSLocale *)locale error:(out NSError **)error;

@end

的NSString + SoftHyphenation.m

#import "NSString+SoftHyphenation.h"

NSString * const NSStringSoftHyphenationErrorDomain = @"NSStringSoftHyphenationErrorDomain";
NSString * const NSStringSoftHyphenationToken = @"­"; // NOTE: UTF-8 soft hyphen!

@implementation NSString (SoftHyphenation)

- (BOOL)canSoftHyphenateStringWithLocale:(NSLocale *)locale
{
    CFLocaleRef localeRef = (__bridge CFLocaleRef)(locale);
    return CFStringIsHyphenationAvailableForLocale(localeRef);
}

- (NSString *)softHyphenatedStringWithLocale:(NSLocale *)locale error:(out NSError **)error
{
    if(![self canSoftHyphenateStringWithLocale:locale])
    {
        if(error != NULL)
        {
            *error = [self hyphen_createOnlyError];
        }
        return [self copy];
    }
    else
    {
        NSMutableString *string = [self mutableCopy];
        unsigned char hyphenationLocations[string.length];
        memset(hyphenationLocations, 0, string.length);
        CFRange range = CFRangeMake(0, string.length);
        CFLocaleRef localeRef = (__bridge CFLocaleRef)(locale);

        for(int i = 0; i < string.length; i++)
        {
            CFIndex location = CFStringGetHyphenationLocationBeforeIndex((CFStringRef)string, i, range, 0, localeRef, NULL);

            if(location >= 0 && location < string.length)
            {
                hyphenationLocations[location] = 1;
            }
        }

        for(int i = string.length - 1; i > 0; i--)
        {
            if(hyphenationLocations[i])
            {

                [string insertString:NSStringSoftHyphenationToken atIndex:i];
            }
        }

        if(error != NULL) { *error = nil; }

        // Left here in case you want to test with visible results
        // return [string stringByReplacingOccurrencesOfString:NSStringSoftHyphenationToken withString:@"-"];
        return string;
    }
}

- (NSError *)hyphen_createOnlyError
{
    NSDictionary *userInfo = @{
                               NSLocalizedDescriptionKey: @"Hyphenation is not available for given locale",
                               NSLocalizedFailureReasonErrorKey: @"Hyphenation is not available for given locale",
                               NSLocalizedRecoverySuggestionErrorKey: @"You could try using a different locale even though it might not be 100% correct"
                               };
    return [NSError errorWithDomain:NSStringSoftHyphenationErrorDomain code:NSStringSoftHyphenationErrorNotAvailableForLocale userInfo:userInfo];
}

@end

:)