我有以下代碼,我正在嘗試使用核心文本進行繪製,這就是爲什麼我無法剪輯像UILabel所做的那樣的文本。換句話說,我必須找出省略號('...')我的自我。向NSString添加省略號
CGSize commentSize = [[self.sizeDictionary_ valueForKey:commentSizeKey] CGSizeValue];
CGSize actualSize = [[self.sizeDictionary_ valueForKey:actualCommentSizeKey] CGSizeValue];
NSString *actualComment = self.highlightItem_.comment;
if (actualSize.height > commentSize.height){
actualComment = [self.highlightItem_.comment stringByReplacingCharactersInRange:NSMakeRange(68, 3) withString:@"..."];
}
我很難找到基於CGSize的'...'的範圍。解決這個問題最好的方法是什麼?
以下是我正在繪製它:
CFStringRef string = CFBridgingRetain(actualComment);
CFMutableAttributedStringRef comment = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (comment ,CFRangeMake(0, 0), string);
CGColorRef blue = CGColorRetain([UIColor colorWithRed:131/255.f green:204/255.f blue:253/255.f alpha:1.0].CGColor);
CGColorRef gray = CGColorRetain([UIColor colorWithWhite:165/255.f alpha:1.0].CGColor);
CFAttributedStringSetAttribute(comment, CFRangeMake(0, [name length]),kCTForegroundColorAttributeName, blue);
CFAttributedStringSetAttribute(comment, CFRangeMake([name length], [self.highlightItem_.comment length] - [name length]),kCTForegroundColorAttributeName, gray);
CGColorRelease (blue);
CGColorRelease (gray);
CTFontRef nameFont = CTFontCreateWithName(CFBridgingRetain(kProximaNovaBold), 13.0f, nil);
CFAttributedStringSetAttribute(comment,CFRangeMake(0, [name length]),kCTFontAttributeName,nameFont);
CTFontRef commentFont = CTFontCreateWithName(CFBridgingRetain(kProximaNovaRegular), 13.0f, nil);
CFAttributedStringSetAttribute(comment, CFRangeMake([name length], [self.highlightItem_.comment length] - [name length]),kCTFontAttributeName,commentFont);
CGFloat commentYOffset = floorf((self.commentHeight_ - commentSize.height)/2);
CGContextSaveGState(context);
CGRect captionFrame = CGRectMake(0, 0, rect.size.width - 80, commentSize.height);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(comment);
CGMutablePathRef captionFramePath = CGPathCreateMutable();
CGPathAddRect(captionFramePath, NULL, captionFrame);
CTFrameRef mainCaptionFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), captionFramePath, NULL);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, self.buttonSize_ + 25, self.imageHeight_ + self.commentHeight_ + 6 - commentYOffset);
CGContextScaleCTM(context, 1.0, -1.0);
CTFrameDraw(mainCaptionFrame, context);
CGContextRestoreGState(context);
你能使用'的NSString drawAtPoint:forWidth: withFont:lineBreakMode:傳遞'NSLineBreakByTruncatingTail'作爲換行符模式的'(或類似的)方法? – rmaddy 2013-02-23 17:35:32
@rmaddy我編輯了上面的帖子來展示我如何繪製文本。我實際上沒有使用drawAtPoint – adit 2013-02-23 17:43:00
我很確定有一個工具可以自動執行省略號,儘管我現在找不到它。 (想着也許NSFont,但我沒有看到它。) – 2013-02-23 19:01:01