【发布时间】:2013-10-31 14:39:58
【问题描述】:
如何在 iOS 中激活自动断字?
我尝试在 UILabel 的属性文本选项中将连字符因子设置为 1,但是我没有得到任何连字符。
【问题讨论】:
-
在这里查看正确答案:stackoverflow.com/questions/6968331/…
-
HTML 中没有开箱即用的东西吗?我不想手动添加软连字符。
标签: ios hyphenation
如何在 iOS 中激活自动断字?
我尝试在 UILabel 的属性文本选项中将连字符因子设置为 1,但是我没有得到任何连字符。
【问题讨论】:
标签: ios hyphenation
UITextView 而不是UILabel。然后hyphenationFactor(作为NSParagraphStyle 属性或NSLayoutManager 属性)应该可以工作(感谢新的TextKit)。UIWebView 和 -webkit-hyphens CSS 属性。CFStringGetHyphenationLocationBeforeIndex() 函数。此函数仅提示您在特定语言的字符串中放置连字符的位置。然后你必须使用核心文本函数(如CTLineCreateWithAttributedString() 和所有)自己打破你的文本行。参见Getting to Know TextKit(名为Hyphenation 的段落解释了Core Text 过程的逻辑,没有代码)和Hyphenation with Core Text on the iPad(给出了一些代码示例,但该网站现在似乎已关闭)。工作量可能会超出您的预期!【讨论】:
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.hyphenationFactor = 1; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"asdf" attributes:[NSDictionary dictionaryWithObjectsAndKeys:paragraph, NSParagraphStyleAttributeName, nil]]; 也在为 UILabel 工作!
hyphenationFactor 设置为 1.0 并且设备的语言设置为非通用语言(即波兰语或克罗地亚语)时,连字词末尾缺少连字符破折号。
您需要在字符串中添加“软连字符”。这些是“-”,在渲染时不可见,而只是排队等待 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
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
【讨论】: