Iphone 断字库不';我无法使用iOS 5
我刚试过图珀的连字符库 这里提到过 但是,虽然它在iOS 4.3下工作得很好,但我没有让它在iOS 5上工作 我还可以使用其他框架吗?我听说过CoreText,但我不知道从哪里开始 提前谢谢Iphone 断字库不';我无法使用iOS 5,iphone,ios,sdk,hyphenation,Iphone,Ios,Sdk,Hyphenation,我刚试过图珀的连字符库 这里提到过 但是,虽然它在iOS 4.3下工作得很好,但我没有让它在iOS 5上工作 我还可以使用其他框架吗?我听说过CoreText,但我不知道从哪里开始 提前谢谢 马丁 < P>我意识到这是几年了,但我发现有一个核心的基础函数,暗示连字点:它只适用于少数几种语言,但看起来它可能真的有助于解决窄标签问题 更新: 下面是一些示例代码。这是一个CLI程序,显示在何处对单词进行连字符: #include <Cocoa/Cocoa.h> int main(int
马丁 < P>我意识到这是几年了,但我发现有一个核心的基础函数,暗示连字点:它只适用于少数几种语言,但看起来它可能真的有助于解决窄标签问题 更新: 下面是一些示例代码。这是一个CLI程序,显示在何处对单词进行连字符:
#include <Cocoa/Cocoa.h>
int main(int ac, char *av[])
{
@autoreleasepool {
if(ac < 2) {
fprintf(stderr, "usage: hyph word\n");
exit(1);
}
NSString *word = [NSString stringWithUTF8String: av[1]];
unsigned char hyspots[word.length];
memset(hyspots, 0, word.length);
CFRange range = CFRangeMake(0, word.length);
CFLocaleRef locale = CFLocaleCreate(NULL, CFSTR("en_US"));
for(int i = 0; i < word.length; i++) {
int x = CFStringGetHyphenationLocationBeforeIndex(
(CFStringRef) word, i, range,
0, locale, NULL);
if(x >= 0 && x < word.length)
hyspots[x] = 1;
}
for(int i = 0; i < word.length; i++) {
if(hyspots[i]) putchar('-');
printf("%s", [[word substringWithRange: NSMakeRange(i, 1)] UTF8String]);
}
putchar('\n');
}
exit(0);
}
这些连字号与OSX字典完全一致。我在iOS中使用了一个窄标签问题,它对我来说很好。 我意识到它已经有几年了,但是我发现有一个核心的基础函数,暗示连字符点:它只适用于少数几种语言,但看起来它可能真的有助于解决窄标签问题 更新: 下面是一些示例代码。这是一个CLI程序,显示在何处对单词进行连字符:
#include <Cocoa/Cocoa.h>
int main(int ac, char *av[])
{
@autoreleasepool {
if(ac < 2) {
fprintf(stderr, "usage: hyph word\n");
exit(1);
}
NSString *word = [NSString stringWithUTF8String: av[1]];
unsigned char hyspots[word.length];
memset(hyspots, 0, word.length);
CFRange range = CFRangeMake(0, word.length);
CFLocaleRef locale = CFLocaleCreate(NULL, CFSTR("en_US"));
for(int i = 0; i < word.length; i++) {
int x = CFStringGetHyphenationLocationBeforeIndex(
(CFStringRef) word, i, range,
0, locale, NULL);
if(x >= 0 && x < word.length)
hyspots[x] = 1;
}
for(int i = 0; i < word.length; i++) {
if(hyspots[i]) putchar('-');
printf("%s", [[word substringWithRange: NSMakeRange(i, 1)] UTF8String]);
}
putchar('\n');
}
exit(0);
}
这些连字号与OSX字典完全一致。我用它来解决iOS中的窄标签问题,对我来说效果很好。我写了一个基于类别的Jeffrey的答案,在任何字符串中添加“软连字符”。这些是“-”,在呈现时不可见,而只是排队等待CoreText或UITextKit知道如何分解单词
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测试和框架工程检查
NSString+softhypernation.h NSString+SoftHyphenation.m
#导入“NSString+SoftHyphenation.h”
NSString*const NSStringSoftHyphenationErrorDomain=@“NSStringSoftHyphenationErrorDomain”;
NSString*const NSStringSoftHyphenationToken=@“…”;//注意:UTF-8软连字符!
@实现NSString(软断字)
-(BOOL)canSoftHyphenateStringWithLocale:(NSLocale*)locale
{
CFLocaleRef localeRef=(桥CFLocaleRef)(locale);
返回CFStringIsHyphenationAvailableForLocale(localeRef);
}
-(NSString*)软连字符StringWithLocale:(NSLocale*)区域设置错误:(out NSError**)错误
{
如果(![self-canSoftHyphenateStringWithLocale:locale])
{
if(错误!=NULL)
{
*错误=[self hyphen_createOnlyError];
}
返回[自复制];
}
其他的
{
NSMutableString*字符串=[self-mutableCopy];
无符号字符连字符位置[string.length];
memset(连字符位置,0,string.length);
CFRange range=CFRangeMake(0,string.length);
CFLocaleRef localeRef=(桥CFLocaleRef)(locale);
for(int i=0;i=0&&location0;i--)
{
if(连字符位置[i])
{
[string insertString:nsstringsofthyphentationtoken-atIndex:i];
}
}
如果(error!=NULL){*error=nil;}
//如果您想用可见的结果进行测试,请将其保留在此处
//返回[string stringByReplacingOccurrencesOfString:NSStringSofthyphenalationToken with string:@“-”];
返回字符串;
}
}
-(N错误*)连字符_createOnlyError
{
NSDictionary*用户信息=@{
NSLocalizedDescriptionKey:@“对于给定的区域设置,断字不可用”,
NSLocalizedFailureReasonErrorKey:@“断字不适用于给定区域设置”,
NSLocalizedRecoverysSuggestionErrorKey:@“您可以尝试使用不同的区域设置,即使它可能不是100%正确”
};
返回[NSErrorWithDomain:NSStringSoftHyphenationErrorDomain代码:NSStringSoftHyphenationErrorNotAvailableForLocale userInfo:userInfo];
}
@结束
:)我写了一个基于类别的Jeffrey的答案,在任何字符串中添加“软连字符”。这些是“-”,在呈现时不可见,而只是排队等待CoreText或UITextKit知道如何分解单词
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测试和框架工程检查
NSString+softhypernation.h NSString+SoftHyphenation.m
#导入“NSString+SoftHyphenation.h”
NSString*const NSStringSoftHyphenationErrorDomain=@“NSStringSoftHyphenationErrorDomain”;
NSString*const NSStringSoftHyphenationToken=@“…”;//注意:UTF-8软连字符!
@实现NSString(软断字)
-(BOOL)canSoftHyphenateStringWithLocale:(NSLocale*)locale
{
CFLocaleRef localeRef=(桥CFLocaleRef)(locale);
返回CFStringIsHyphenationAvailableForLocale(localeRef);
}
-(NSString*)软连字符StringWithLocale:(NSLocale*)区域设置错误:(out NSError**)错误
{
如果(![self-canSoftHyphenateStringWithLocale:locale])
{
if(错误!=NULL)
{
*错误=[self hyphen_createOnlyError];
}
返回[自复制];
}
其他的
{
NSMutableString*字符串=[self-mutableCopy];
无符号字符连字符位置[string.length];
memset(连字符位置,0,string.length);
CFRange range=CFRangeMake(0,string.length);
CFLocaleRef localeRef=(桥CFLocaleRef)(locale);
for(int i=0;i=0&&location#import "NSString+SoftHyphenation.h"
NSString * const NSStringSoftHyphenationErrorDomain = @"NSStringSoftHyphenationErrorDomain";
NSString * const NSStringSoftHyphenationToken = @""; // NOTE: UTF-8 soft hyphen!
@implementation NSString (SoftHyphenation)
- (BOOL)canSoftHyphenateStringWithLocale:(NSLocale *)locale
{
CFLocaleRef localeRef = (__bridge CFLocaleRef)(locale);
return CFStringIsHyphenationAvailableForLocale(localeRef);
}
- (NSString *)softHyphenatedStringWithLocale:(NSLocale *)locale error:(out NSError **)error
{
if(![self canSoftHyphenateStringWithLocale:locale])
{
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);
CFLocaleRef localeRef = (__bridge CFLocaleRef)(locale);
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:NSStringSoftHyphenationToken atIndex:i];
}
}
if(error != NULL) { *error = nil; }
// Left here in case you want to test with visible results
// return [string stringByReplacingOccurrencesOfString:NSStringSoftHyphenationToken withString:@"-"];
return string;
}
}
- (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];
}
@end