IOS 根据 指定的 字体类型 ,size ,计算 绘制 文本 所需要的 宽度与高度
2012-10-24 14:03
591 查看
IOS 根据 指定的 字体类型 ,size ,计算 绘制 文本 所需要的 宽度与高度
分类: IOS 应用2012-09-1320:15 162人阅读 评论(0) 收藏 举报
Iphone 允许 我们直接用于字体实例变量 的 字体类型(NSString) 有 :
“Arial”,
“Helvetica”,
"Georgia",
"Courier New",
"Marker Felt",
"Times New Roman",
"Trebuchet MS",
"Verdana",
"Zapfino"。
[cpp] view
plaincopy
- (void)computeWidthAndHeight:(CGContextRef) context
withFont:(NSString *)fontName
withSize:(NSInteger)fontSize
string:(NSString *)str
{
int strLength = [str length];
CGFontRef fontRef = CGFontCreateWithFontName((CGStringRef) fontName); // create a system font ref
if (!fontRef)
{
NSLog(@"Warning : missing font %@",fontName);
return;
}
CGRect bbox = CGFontGetFontBBox(fontRef); // return a box that can contain any char with this font
int units = CGFontGetUnitsPerEm(fontRef); // return how many glyph unit represent a system device unit
CGFloat height = (((float)bbox.size.height)/((float)units))*fontSize; // compute the char height
CGContextSaveGState(context); // save current context state
// use the invisible way to draw the str
CGPoint left = CGContextGetTextPosition(context); // get the current context position
CGContextSetTextDrawingMode(context, kCGTextInvisible); // 1 step
CGContextSetTextMatrix(context, CGAffineTransformIdentity); // 2 step
CGContextSelectFont(context, [font UTF8String], fontSize, KCGEncodingMacRoman); // 3 step
// CGContextSetRGBStrokeColor(); // 4 step can be omitted
// CGContextSetRGBFillColor();
CGContextShowText(context, [str UTF8String], strLength); // 5 step, default at current position
// CGContextShowTextAtPoint(); // alse can work
CGPoint right = CGContextGetTextPosition(context); // get the end context position
CGContextRestoreGState(context); // restore the before context state
CGFolat width = right.x - left.x; // compute the char width
CGFontRelease(fontRef); // release font ref
}
相关文章推荐
- IOS 根据 指定的 字体类型 ,size ,计算 绘制 文本 所需要的 宽度与高度
- IOS计算UILabel单行高度、计算UILabel多行文本高度、宽度的技巧,根据文本的高度设置cell高度
- [置顶] UILabel根据文本、字体大小计算label宽度;以及自适应高度
- iOS开发之----根据文本内容和字体大小计算UILabel的size
- iOS小demo之根据label的宽度计算字符串显示需要的高度
- 根据text字体类型计算全部高度
- iOS根据字体字号大小和字体label计算label宽度
- iOS开 4000 发时关于UILabel文本高度和宽度的计算问题(单行、多行)
- IOS BUG记录 boundingRectWithSize计算内容宽度高度的问题
- iOS开发动态计算cell高度或UILabel多行文本计算高度、宽度的技巧
- IOS core text计算文本高度及最大宽度
- 字符串在指定区域内按照指定的字体显示时,需要的高度和宽度
- IOS开发中UILabel单行、多行文本计算高度、宽度的技巧
- iOS UILabel根据文本宽度改变字体大小
- iOS计算NSMutableAttributedString、NSString指定宽度前提下的高度
- ios 动态计算文本的高度或宽度
- IOS聊天泡泡,根据内容计算宽度和高度(动态计算高度和宽度)
- 指定宽度,根据字体获得高度。
- ios开发根据Label的字体和宽度算出它的高度
- IOS开发中UILabel单行、多行文本计算高度、宽度的技巧