ios – 使用CFStringGetHyphenationLocationBeforeIndex添加连字符

ios – 使用CFStringGetHyphenationLocationBeforeIndex添加连字符,第1张

概述我正在制作一本带有Core Text的杂志,我正在尝试自动为文本添加字符.我想我可以用这个功能做到这一点 CFStringGetHyphenationLocationBeforeIndex 但它没有用,我在网上找不到任何例子. 我正在尝试设置文本,如果我找到任何连字符位置,我在其中放置一个“ – ”并通过访问器替换文本,因此它调用setNeedsDisplay并从头开始绘制. - (void) 我正在制作一本带有Core Text的杂志,我正在尝试自动为文本添加连字符.我想我可以用这个功能做到这一点

CFStringGetHyphenationLocationBeforeIndex

但它没有用,我在网上找不到任何例子.

我正在尝试设置文本,如果我找到任何连字符位置,我在其中放置一个“ – ”并通过访问器替换文本,因此它调用setNeedsdisplay并从头开始绘制.

- (voID) setTexto:(NSAttributedString *)texto {    _texto = texto;    if (self.ctFrame != NulL) {        CFRelease(self.ctFrame);        self.ctFrame = NulL;    }    [self setNeedsdisplay];}-(voID)drawRect:(CGRect)rect {    CGContextRef context = UIGraphicsGetCurrentContext();       CGContextSetTextMatrix(context,CGAffinetransformIDentity);    CGContextScaleCTM(context,1.0,-1.0);    CGMutablePathref path = CGPathCreateMutable();    CGPathAddRect(path,NulL,self.bounds);    CTFramesetterRef ctFrameSetterRef = CTFramesetterCreateWithAttributedString((__brIDge CFAttributedStringRef)(_texto));    self.ctFrame = CTFramesetterCreateFrame(ctFrameSetterRef,CFRangeMake(0,[_texto length]),path,NulL);    NSArray *lines = (__brIDge NSArray *)(CTFrameGetlines(self.ctFrame));    CFIndex lineCount = [lines count];    NSLog(@"lines: %d",lines.count);    for(CFIndex IDx = 0; IDx < lineCount; IDx++) {        CTlineRef line = CFArrayGetValueAtIndex((CFArrayRef)lines,IDx);        CFRange linestringRange = CTlineGetStringRange(line);        NSRange lineRange = NSMakeRange(linestringRange.location,linestringRange.length);        Nsstring* linestring = [self.texto.string substringWithRange:lineRange];        CFStringRef localeIDent = CFSTR("es_ES");        CFLocaleRef localeRef = CFLocaleCreate(kcfAllocatorDefault,localeIDent);        NSUInteger breakAt = CFStringGetHyphenationLocationBeforeIndex((__brIDge CFStringRef)linestring,lineRange.length,lineRange.length),localeRef,NulL);        if(breakAt!=-1) {            NSRange replaceRange = NSMakeRange(lineRange.location+breakAt,0);            NSMutableAttributedString* attributedString = self.texto.mutablecopy;            [attributedString replaceCharactersInRange:replaceRange withAttributedString:[[NSAttributedString alloc] initWithString:@"-"]];            self.texto = attributedString.copy;            break;        } else {            CGfloat ascent;            CGfloat descent;            CTlineGetTypographicBounds(line,&ascent,&descent,nil);            CGContextSetTextposition(context,0.0,IDx*-(ascent - 1 + descent)-ascent);            CTlineDraw(line,context);        }    }}

问题是它第二次进入drawRect(使用新文本)lines.count log为0.而且我也不确定这是正确的方法.

也许有另一种修改CTlines的方法(在前一行的“ – ”之后添加剩余的字符).

解决方法 在创建属性字符串之前,您可以添加软连字符.然后你可以做绘图.

我写了一个类别,为任何字符串添加“soft hyphenation”.这些是“ – ”,在渲染时不可见,而只是排队等待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 SoftHyphenation.h

typedef enum {    NsstringSoftHyphenationErrorNotAvailableForLocale} NsstringSoftHyphenationError;extern Nsstring * const NsstringSoftHyphenationErrorDomain;extern Nsstring * const NsstringSoftHyphenationToken;@interface Nsstring (SoftHyphenation)- (BOol)canSoftHyphenateStringWithLocale:(NSLocale *)locale;- (Nsstring *)softHyphenatedStringWithLocale:(NSLocale *)locale error:(out NSError **)error;@end

Nsstring SoftHyphenation.m

#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,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,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

希望这可以帮助 :)

总结

以上是内存溢出为你收集整理的ios – 使用CFStringGetHyphenationLocationBeforeIndex添加连字符全部内容,希望文章能够帮你解决ios – 使用CFStringGetHyphenationLocationBeforeIndex添加连字符所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1065061.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存