在原生iOS应用程序中进行连字
我如何激活iOS中的自动连字?
我试图在UILabel的属性文本选项中将连字符因子设置为1,但是我没有得到任何连字符。
- iOS 7的方式 。 使用
UITextView
而不是UILabel
。hyphenationFactor
(作为NSParagraphStyle
属性或作为NSLayoutManager
属性)应该工作(感谢新的TextKit)。 - Web方式 。 使用
UIWebView
和-webkit-hyphens
CSS属性。 - 核心文本或艰难的方式 。 使用您在注释中提到的
CFStringGetHyphenationLocationBeforeIndex()
函数。 这个函数只给你一个关于在哪里把连字符放到一个特定语言的string中的暗示。 然后你必须使用核心文本函数(比如CTLineCreateWithAttributedString()
和all)来自行CTLineCreateWithAttributedString()
文本。 请参阅了解TextKit (称为“连字符”的段落解释了核心文本过程的逻辑,无代码)和iPad上的核心文本连字符 (提供了一些代码示例,但网站似乎现在正在运行)。 这可能会比你想要的更多的工作!
CoreText或TextKit
您需要在string中添加“软连字符”。 这些是“ – ”,在呈现时不可见,而是仅仅排队等待CoreText或UITextKit来知道如何分解单词。
你应该在文本中放置的软连字符是:
unichar const kTextDrawingSoftHyphenUniChar = 0x00AD; NSString * const kTextDrawingSoftHyphenToken = @""; // NOTE: UTF-8 soft hyphen!
示例代码
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; @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
Swift版本:
let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.hyphenationFactor = 1 paragraphStyle.alignment = .center let string = NSAttributedString(string: "wyindywidualizowany indywidualista".uppercased(), attributes: [NSParagraphStyleAttributeName : paragraphStyle]) myLabel.attributedText = string