我该如何在iOS中启用自动断字功能?
我尝试在UILabel的属性文本选项中将断字因子设置为1,但是并没有出现任何连字符。
我该如何在iOS中启用自动断字功能?
我尝试在UILabel的属性文本选项中将断字因子设置为1,但是并没有出现任何连字符。
UITextView
代替UILabel
。然后可以使用hyphenationFactor
(作为NSParagraphStyle
属性或者NSLayoutManager
属性)来实现自动分词(感谢新的TextKit)。UIWebView
和-webkit-hyphens
CSS属性。CFStringGetHyphenationLocationBeforeIndex()
函数,该函数只能针对特定语言给出字符串中放置连字符的提示信息。然后需要使用Core Text函数(如CTLineCreateWithAttributedString()
等)手动将文本行进行断行处理。可以参考Getting to Know TextKit (其中的 Hyphenation 段落介绍了Core Text处理逻辑,但没有代码) 和 iPad上使用Core Text实现连字符 (提供了一些代码示例,但是该网站目前似乎已经无法访问)。这可能比您想要的更费力!您需要在字符串中添加“软连字”。这些是“ - ”,当呈现时不可见,而仅仅是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);
输出 可访问性测试和框架检查
typedef enum {
NSStringSoftHyphenationErrorNotAvailableForLocale
} NSStringSoftHyphenationError;
extern NSString * const NSStringSoftHyphenationErrorDomain;
@interface NSString (SoftHyphenation)
- (NSString *)softHyphenatedStringWithLocale:(NSLocale *)locale error:(out NSError **)error;
@end
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