Macos 根据xcode中的文本、字体和文本颜色计算高度和宽度
计算固定UIView(包括换行等)中文本字段的适当高度和宽度(格式化文本,包括文本大小、文本颜色、文本字体)的最佳方法是什么 在下图中,文本未右对齐 在下图中,文本未正确显示 我用下面的代码计算图像的高度和宽度Macos 根据xcode中的文本、字体和文本颜色计算高度和宽度,macos,height,width,Macos,Height,Width,计算固定UIView(包括换行等)中文本字段的适当高度和宽度(格式化文本,包括文本大小、文本颜色、文本字体)的最佳方法是什么 在下图中,文本未右对齐 在下图中,文本未正确显示 我用下面的代码计算图像的高度和宽度 NSString* text=[textField stringValue]; NSDictionary *attributes; NSTextView* textView =[[NSTextView alloc] init]; [textView set
NSString* text=[textField stringValue];
NSDictionary *attributes;
NSTextView* textView =[[NSTextView alloc] init];
[textView setString:text];
attributes = @{NSFontAttributeName : [NSFont fontWithName:fontName size:fontValue], NSForegroundColorAttributeName : [NSColor colorWithCalibratedRed:redValueTextColor green:GreenValueTextColor blue:blueValueTextColor alpha:1], NSBackgroundColorAttributeName : [NSColor colorWithCalibratedRed:redValueTextBackgroundColor green:GreenValueTextBackgroundColor blue:blueValueTextBackgroundColor alpha:1]};
textView.backgroundColor=[NSColor colorWithCalibratedRed:redValueTextBackgroundColor green:GreenValueTextBackgroundColor blue:blueValueTextBackgroundColor alpha:1];
NSInteger maxWidth = 600;
NSInteger maxHeight = 20000;
CGSize constraint = CGSizeMake(maxWidth, maxHeight);
NSRect newBounds = [text boundingRectWithSize:constraint options:NSLineBreakByCharWrapping|NSStringDrawingUsesFontLeading attributes:attributes];
textView.frame = NSMakeRect(textView.frame.origin.x, textView.frame.origin.y, newBounds.size.width, newBounds.size.height);
textView =[NSColor colorWithCalibratedRed:redValueTextColor green:GreenValueTextColor blue:blueValueTextColor alpha:1];
[textView setFont:[NSFont fontWithName:fontName size:fontValue]];
核心文本解决方案(注意,如果需要,maxWidth允许换行):
(CGSize)sizeForText:(NSAttributedString*)字符串最大宽度:(CGFloat)宽度
{
CTTypesetterRef typesetter=CTTypesetterCreateWithAttributedString(((u桥CFAttributedStringRef)字符串);
CFIndex offset=0,长度;
cGy=0,线宽;
做{
长度=CTTypesetterSuggestLineBreak(排字机、偏移量、宽度);
CTLineRef line=CTTypesetterCreateLine(排字机,CFRangeMake(偏移量,长度));
上升、下降、领先;
CtlineGettyGraphicBounds(直线、上升、下降和领先);
释放(线);
偏移量+=长度;
上升=圆形(上升);
下降=圆形(下降);
超前=圆形(超前);
线宽=上升+下降+领先;
lineHeight=lineHeight+((前导>0)?0:roundf(0.2*lineHeight));//添加20%的空间
y+=线宽;
}而(偏移量<[字符串长度]);
排字机;
返回CGSizeMake(宽度,y);
}
如果颜色影响文本大小,那就很奇怪了。:)我不知道,但对我来说,CTTypesetterSuggestLineBreak返回整个字符串的长度。我遗漏了什么吗?宽度参数是建议CTTypesetterSuggestLineBreak指定文本的最大宽度。然后,它返回一个基于断开(包装)字符串的长度,以便它符合提供的宽度。嗨,我解决了我的问题。我是从NSCell cellSize
获取宽度,而不是从titlelectforbounds:
返回的大小。
(CGSize)sizeForText:(NSAttributedString *)string maxWidth:(CGFloat)width
{
CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)string);
CFIndex offset = 0, length;
CGFloat y = 0, lineHeight;
do {
length = CTTypesetterSuggestLineBreak(typesetter, offset, width);
CTLineRef line = CTTypesetterCreateLine(typesetter, CFRangeMake(offset, length));
CGFloat ascent, descent, leading;
CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
CFRelease(line);
offset += length;
ascent = roundf(ascent);
descent = roundf(descent);
leading = roundf(leading);
lineHeight = ascent + descent + leading;
lineHeight = lineHeight + ((leading > 0) ? 0 : roundf(0.2*lineHeight)); //add 20% space
y += lineHeight;
} while (offset < [string length]);
CFRelease(typesetter);
return CGSizeMake(width, y);
}