gpt4 book ai didi

objective-c - Core Text 比 NSString drawInRect :? 小一点

转载 作者:技术小花猫 更新时间:2023-10-29 11:06:06 27 4
gpt4 key购买 nike

理论上,它们的大小应该相同,但它们不是:

蓝色文本来自Core Text,黑色文本来自-[NSString drawInRect:]。这是代码:

//Some formatting to get the correct frame
int y = MESSAGE_FRAME.origin.y + 8;

if (month) y = y + 27;

int height = [JHomeViewCellContentView heightOfMessage:self.entry.message];

CGRect rect = CGRectMake(MESSAGE_FRAME.origin.x + 8, y, MESSAGE_FRAME.size.width - 16, height);


//Draw in rect method
UIFont *font = [UIFont fontWithName:@"Crimson" size:15.0f];

[[UIColor colorWithWhite:0.2 alpha:1.0] setFill];

[self.entry.message drawInRect:rect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];



//Frame change to suit CoreText
CGRect rect2 = CGRectMake(MESSAGE_FRAME.origin.x + 8, self.bounds.size.height - y - height, MESSAGE_FRAME.size.width - 16, height);

//Core text method
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"Crimson", 15.0f, NULL);

NSDictionary *attDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge_transfer id)fontRef, (NSString *)kCTFontAttributeName,
(id)[[UIColor blueColor] CGColor], (NSString *)kCTForegroundColorAttributeName,
nil];

NSAttributedString *attString = [[NSAttributedString alloc] initWithString:self.entry.message attributes:attDictionary];

//Flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);



CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, rect2);

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge_retained CFAttributedStringRef)attString);
CTFrameRef theFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [attString length]), path, NULL);
CFRelease(framesetter);
CFRelease(path);

CTFrameDraw(theFrame, context);
CFRelease(theFrame);

字体是一样的。我不明白为什么呈现不同。

最佳答案

这是因为 NSLayoutManager 使用了一些不寻常的启发式方法来处理某些字体。

参见 How does line spacing work in Core Text? (and why is it different from NSLayoutManager?)了解更多详情。

关于objective-c - Core Text 比 NSString drawInRect :? 小一点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10587606/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com