Migrate in-app for native iOS apps

How to activate automatic transfer in iOS?

I tried setting the hyphenation factor to 1 in the attribute parameters of the UILabel text, but nonetheless, I am not getting any hyphens.

+8
ios
source share
3 answers
  • IOS 7 method . Use UITextView instead of UILabel . hyphenationFactor (either as an NSParagraphStyle attribute, or as an NSLayoutManager property) should work then (thanks to the new TextKit).
  • Web path . Use the CSS properties of UIWebView and -webkit-hyphens .
  • Body text or hard way . Use the CFStringGetHyphenationLocationBeforeIndex() function specified in the comment. This function only gives you a hint about where to put hyphens in a string for a particular language. Then you must break your lines of text yourself using Core Text functions (for example, CTLineCreateWithAttributedString() and that's it). See Introducing TextKit (a paragraph titled Hyphenation explains the logic behind the Core Text process without code) and porting with body text to the iPad (gives sample code, but the site seems to be down now). This will probably be more work than you want!
+11
source share

CoreText or TextKit

You need to add a soft wrap to the string. These are “-” that are not visible during visualization, but instead are just queues for CoreText or UITextKit to know how to break words.

The soft hyphen icon you should put in the text:

 unichar const kTextDrawingSoftHyphenUniChar = 0x00AD; NSString * const kTextDrawingSoftHyphenToken = @""; // NOTE: UTF-8 soft hyphen! 

Code example

 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; @interface NSString (SoftHyphenation) - (NSString *)softHyphenatedStringWithLocale:(NSLocale *)locale error:(out NSError **)error; @end 

NSString + SoftHyphenation.m

 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 < 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:@"-" atIndex:i]; } } if(error != NULL) { *error = nil;} return string; } } @end 
+8
source share

Quick version:

 let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.hyphenationFactor = 1 paragraphStyle.alignment = .center let string = NSAttributedString(string: "wyindywidualizowany indywidualista".uppercased(), attributes: [NSParagraphStyleAttributeName : paragraphStyle]) myLabel.attributedText = string 
0
source share

All Articles