如何在iOS中激活自动连字?
我试图在UILabel的属性文本选项中将连字因子设置为1,但是我没有得到任何连字符。
答案 0 :(得分:13)
UITextView
代替UILabel
。 hyphenationFactor
(作为NSParagraphStyle
属性或作为NSLayoutManager
属性)应该可以使用(感谢新的TextKit)。UIWebView
和-webkit-hyphens
CSS属性。CFStringGetHyphenationLocationBeforeIndex()
功能。此函数仅为您提供有关在特定语言的字符串中将连字符放在何处的提示。然后你必须使用核心文本函数(如CTLineCreateWithAttributedString()
和所有)来破坏你自己的文本行。请参阅Getting to Know TextKit(称为连字符的段落解释了核心文本流程的逻辑,没有代码)和Hyphenation with Core Text on the iPad(提供了一些代码示例,但网站似乎现在已经关闭了)。它可能比你想要的更多工作!答案 1 :(得分:9)
您需要在字符串中添加“soft hyphenation”。这些是“ - ”,在渲染时不可见,而只是排队等待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
答案 2 :(得分:2)
Swift版本:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.hyphenationFactor = 1
paragraphStyle.alignment = .center
let string = NSAttributedString(string: "wyindywidualizowany indywidualista".uppercased(),
attributes: [NSParagraphStyleAttributeName : paragraphStyle])
myLabel.attributedText = string