Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>CoreText or TextKit</h2> <p>You need to add "soft hyphenation" to the string. These are "-" which is not visible when rendered, but instead merely queues for CoreText or UITextKit to know how to break up words.</p> <p>The soft hyphen sign which you should place in the text is:</p> <pre><code>unichar const kTextDrawingSoftHyphenUniChar = 0x00AD; NSString * const kTextDrawingSoftHyphenToken = @"­"; // NOTE: UTF-8 soft hyphen! </code></pre> <p>Example code</p> <pre><code>NSString *string = @"accessibility tests and frameworks checking"; NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US"]; NSString *hyphenatedString = [string softHyphenatedStringWithLocale:locale error:nil]; NSLog(@"%@", hyphenatedString); </code></pre> <p>Outputs <code>ac-ces-si-bil-i-ty tests and frame-works check-ing</code></p> <hr> <h3>NSString+SoftHyphenation.h</h3> <pre><code>typedef enum { NSStringSoftHyphenationErrorNotAvailableForLocale } NSStringSoftHyphenationError; extern NSString * const NSStringSoftHyphenationErrorDomain; @interface NSString (SoftHyphenation) - (NSString *)softHyphenatedStringWithLocale:(NSLocale *)locale error:(out NSError **)error; @end </code></pre> <h3>NSString+SoftHyphenation.m</h3> <pre><code>NSString * const NSStringSoftHyphenationErrorDomain = @"NSStringSoftHyphenationErrorDomain"; @implementation NSString (SoftHyphenation) - (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]; } - (NSString *)softHyphenatedStringWithLocale:(NSLocale *)locale error:(out NSError **)error { CFLocaleRef localeRef = (__bridge CFLocaleRef)(locale); if(!CFStringIsHyphenationAvailableForLocale(localeRef)) { 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); for(int i = 0; i &lt; string.length; i++) { CFIndex location = CFStringGetHyphenationLocationBeforeIndex((CFStringRef)string, i, range, 0, localeRef, NULL); if(location &gt;= 0 &amp;&amp; location &lt; string.length) { hyphenationLocations[location] = 1; } } for(int i = string.length - 1; i &gt; 0; i--) { if(hyphenationLocations[i]) { [string insertString:@"-" atIndex:i]; } } if(error != NULL) { *error = nil;} return string; } } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload