1

I just tried the hyphenate library of Tupil.

It was mentioned here http://blog.tupil.com/adding-hyphenation-to-nsstring/.

But while it is working perfectly under iOS 4.3, I did not get it to work with iOS 5.

Are there any other frameworks I could use? I heard of CoreText, but I don't know where to start.

Thanks in advance Martin

Martin Reichl
  • 932
  • 9
  • 13

2 Answers2

1

I realize it's been a few years, but I just found that there's a Core Foundation function that suggests hyphenation points: CFStringGetHyphenationLocationBeforeIndex. It only works for a few languages, but it looks like it might be really helpful for the narrow label problem.

Update:

Here is some example code. It's a CLI program that shows where to hyphenate a word:

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

Here's how it looks when you build and run it:

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

These hyphenations agree exactly with the OS X dictionary. I am using this for a narrow label problem in iOS, and it's working well for me.

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
0

I wrote a category based Jeffrey's answer for adding "soft hyphenation" to any string. These are "-" which is not visible when rendered, but instead merely queues for CoreText or UITextKit to know how to break up words.

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

Outputs 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

:)

hfossli
  • 22,616
  • 10
  • 116
  • 130