Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Adding hyphens with CFStringGetHyphenationLocationBeforeIndex

I'm making a magazine with Core Text and I'm trying to automatically add hyphens to the text. I think I can do this with the function


But it's not working and I can't find any examples online.

What I'm trying to do is setting up the text and if I find any hyphen position I put a "-" there and replace the text via the accessor so it calls setNeedsDisplay and draws again from scratch.

- (void) setTexto:(NSAttributedString *)texto {

    _texto = texto;

    if (self.ctFrame != NULL) {
        self.ctFrame = NULL;

    [self setNeedsDisplay];

-(void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();   
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.bounds);
    CTFramesetterRef ctFrameSetterRef = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(_texto));
    self.ctFrame = CTFramesetterCreateFrame(ctFrameSetterRef, CFRangeMake(0, [_texto length]), path, NULL);

    NSArray *lines = (__bridge NSArray *)(CTFrameGetLines(self.ctFrame));

    CFIndex lineCount = [lines count];
    NSLog(@"lines: %d", lines.count);

    for(CFIndex idx = 0; idx < lineCount; idx++) {

        CTLineRef line = CFArrayGetValueAtIndex((CFArrayRef)lines, idx);

        CFRange lineStringRange = CTLineGetStringRange(line);
        NSRange lineRange = NSMakeRange(lineStringRange.location, lineStringRange.length);

        NSString* lineString = [self.texto.string substringWithRange:lineRange];

        CFStringRef localeIdent = CFSTR("es_ES");
        CFLocaleRef localeRef = CFLocaleCreate(kCFAllocatorDefault, localeIdent);

        NSUInteger breakAt = CFStringGetHyphenationLocationBeforeIndex((__bridge CFStringRef)lineString, lineRange.length, CFRangeMake(0, lineRange.length), 0, localeRef, NULL);

        if(breakAt!=-1) {
            NSRange replaceRange = NSMakeRange(lineRange.location+breakAt, 0);
            NSMutableAttributedString* attributedString = self.texto.mutableCopy;
            [attributedString replaceCharactersInRange:replaceRange withAttributedString:[[NSAttributedString alloc] initWithString:@"-"]];
            self.texto = attributedString.copy;
        } else {
            CGFloat ascent;
            CGFloat descent;
            CTLineGetTypographicBounds(line, &ascent, &descent, nil);
            CGContextSetTextPosition(context, 0.0, idx*-(ascent - 1 + descent)-ascent);
            CTLineDraw(line, context);

The problem is the second time it goes into drawRect (with the new text) lines.count log to 0. And also I'm not sure this is the correct way to do it.

Maybe there's another way to modify the CTLines (adding the remaining characters after the "-" from the previous line).

like image 275
Odrakir Avatar asked Mar 25 '23 07:03


1 Answers

Before creating the attributed string you could add soft hyphenation. Then you could do the drawing.

I wrote a category 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


typedef enum {
} NSStringSoftHyphenationError;

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

@interface NSString (SoftHyphenation)

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



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

                [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];


Hope this helps :)

like image 133
hfossli Avatar answered Apr 02 '23 04:04