我正在尝试根据长时间NSAttributedString
拆分成的部分来动态创建书页。
我现在正在做的是将这个类别用于NSAttributedString
:
@interface NSAttributedString (Height)
- (CGFloat)boundingHeightForWidth:(CGFloat)inWidth;
@end
@implementation NSAttributedString (Height)
- (CGFloat)boundingHeightForWidth:(CGFloat)inWidth
{
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFMutableAttributedStringRef)self);
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(inWidth, 10000), NULL);
CFRelease(framesetter);
return suggestedSize.height ;
}
@end
由于我使用 a 绘制文本CTFramesetter
,因此效果很好,并且正确返回了框的正确高度。不幸的是,现在我需要逐行绘制文本:
-(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
// Flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
//CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
NSArray *lines = (__bridge NSArray *)(CTFrameGetLines(ctFrame));
CFIndex lineCount = [lines count];
for(CFIndex idx = 0; idx < lineCount; idx++)
{
// For each line found from where it starts and it's length
CTLineRef line = CFArrayGetValueAtIndex((CFArrayRef)lines, idx);
CFRange lineStringRange = CTLineGetStringRange(line);
NSRange lineRange = NSMakeRange(lineStringRange.location, lineStringRange.length);
// Get the line related string
NSString* lineString = [displayedString.string substringWithRange:lineRange];
// Calculate it's range
NSRange stringRange = NSMakeRange(lineStringRange.location, lineStringRange.length);
static const unichar softHypen = 0x00AD;
// Get the last char of the line
unichar lastChar = [lineString characterAtIndex:stringRange.length-1];
// Check if it's a soft hyphenation character
if(softHypen == lastChar) {
NSMutableAttributedString* lineAttrString = [[displayedString attributedSubstringFromRange:stringRange] mutableCopy];
NSRange replaceRange = NSMakeRange(stringRange.length-1, 1);
// Replace it with an hard hyphenation character
[lineAttrString replaceCharactersInRange:replaceRange withString:@"-"];
CTLineRef hyphenLine = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)lineAttrString);
CTLineRef justifiedLine = CTLineCreateJustifiedLine(hyphenLine, 1.0, self.frame.size.width);
CGFloat ascent;
CGFloat descent;
// Calculate the line height
CTLineGetTypographicBounds(justifiedLine, &ascent, &descent, NULL);
// Set the correct position for the line
CGContextSetTextPosition(context, 0.0, idx*-(ascent + descent)-ascent);
CTLineDraw(justifiedLine, context);
}
else{
CGFloat ascent;
CGFloat descent;
// Calculate the line height
CTLineGetTypographicBounds(line, &ascent, &descent, nil);
CGFloat y = idx*-(ascent + descent)-ascent;
// Set the correct position for the line
CGContextSetTextPosition(context, 0.0, y);
CTLineDraw(line, context);
}
}
这几乎可以正常工作,但并非总是如此。有时会发生这种情况:
如您所见,最后一行被剪掉了。有人有什么建议可以解决这个烦人的问题吗?