
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,[name length]),kCTForegroundcolorAttributename,blue); CFAttributedStringSetAttribute(comment,CFRangeMake([name length],[self.highlightItem_.comment length] - [name length]),gray); CGcolorRelease (blue); CGcolorRelease (gray); CTFontRef nameFont = CTFontCreateWithname(CFBrIDgingRetain(kProximaNovaBold),13.0f,nil); CFAttributedStringSetAttribute(comment,kCTFontAttributename,nameFont); CTFontRef commentFont = CTFontCreateWithname(CFBrIDgingRetain(kProximaNovaRegular),commentFont); CGfloat commentYOffset = floorf((self.commentHeight_ - commentSize.height)/2); CGContextSaveGState(context); CGRect captionFrame = CGRectMake(0,rect.size.wIDth - 80,commentSize.height); CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(comment); CGMutablePathref captionFramePath = CGPathCreateMutable(); CGPathAddRect(captionFramePath,NulL,captionFrame); CTFrameRef mainCaptionFrame = CTFramesetterCreateFrame(framesetter,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);解决方法 编辑
(我在这里的原始答案没有用;它没有处理多行.如果有人想看到它的历史兴趣,请查看编辑历史.我删除了它,因为它导致比它解决的更多混乱.答案是正确的代码.)
你需要做的是让CTFramesetter解决除最后一行之外的所有行.然后,如果需要,您可以手动截断最后一个.
- (voID)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetTextMatrix(context,CGAffinetransformIDentity); CGRect pathRect = CGRectMake(50,200,40); CGPathref path = CGPathCreateWithRect(pathRect,NulL); CFAttributedStringRef attrString = (__brIDge CFTypeRef)[self attributedString]; // Create the framesetter using the attributed string CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString); // Create a single frame using the entire string (CFRange(0,0)) // that fits insIDe of path. CTFrameRef frame = CTFramesetterCreateFrame(framesetter,path,NulL); // Draw the lines except the last one CFArrayRef lines = CTFrameGetlines(frame); CFIndex lineCount = CFArrayGetCount(lines); CGPoint origins[lineCount]; // I'm assuming that a stack variable is safe here. // This would be bad if there were thousdands of lines,but that's unlikely. CTFrameGetlineOrigins(frame,origins); for (CFIndex i = 0; i < lineCount - 1; ++i) { CGContextSetTextposition(context,pathRect.origin.x + origins[i].x,pathRect.origin.y + origins[i].y); CTlineDraw(CFArrayGetValueAtIndex(lines,i),context); } /// /// HERE'S THE INTERESTING PART /// // Make a new last line that includes the rest of the string. // The last line is already truncated (just with no truncation mark),so we can't truncate it again CTlineRef lastline = CFArrayGetValueAtIndex(lines,lineCount - 1); CFIndex lastLocation = CTlineGetStringRange(lastline).location; CFRange restRange = CFRangeMake(lastLocation,CFAttributedStringGetLength(attrString) - lastLocation); CFAttributedStringRef restOfString = CFAttributedStringCreateWithSubstring(NulL,attrString,restRange); CTlineRef restline = CTlineCreateWithAttributedString(restOfString); // We need to provIDe the truncation mark. This is an ellipsis (Cmd-semicolon). // You Could also use "\u2026". Don't use dot-dot-dot. It'll work,it's just not correct. // ObvIoUsly you Could cache this… CTlineRef ellipsis = CTlineCreateWithAttributedString((__brIDge CFTypeRef) [[NSAttributedString alloc] initWithString:@"…"]); // OK,Now let's truncate it and draw it. I'm being a little sloppy here. If ellipsis Could possibly // be wIDer than the path wIDth,then this will fail and truncateline will be NulL and we'll crash. // Don't do that. CTlineRef truncatedline = CTlineCreateTruncatedline(restline,CGRectGetWIDth(pathRect),kCTlineTruncationEnd,ellipsis); CGContextSetTextposition(context,pathRect.origin.x + origins[lineCount - 1].x,pathRect.origin.y + origins[lineCount - 1].y); CTlineDraw(truncatedline,context); CFRelease(truncatedline); CFRelease(ellipsis); CFRelease(restline); CFRelease(restOfString); CFRelease(frame); CFRelease(framesetter); CGPathRelease(path);} 总结 以上是内存溢出为你收集整理的iphone – 向NSString添加省略号全部内容,希望文章能够帮你解决iphone – 向NSString添加省略号所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)