iOS 计算字符串的高度类
2015-08-04 11:25
615 查看
#import <Foundation/Foundation.h>
@interface StringHeight :
NSObject
+ (CGFloat)heightWithText:(NSString *)text font:(UIFont *)font constrainedToWidth:(CGFloat)width;
@end
#import "StringHeight.h"
#import <CoreText/CoreText.h>
@implementation StringHeight
+ (CGFloat)heightWithText:(NSString *)text font:(UIFont *)font constrainedToWidth:(CGFloat)width
{
if (!text) {
return 0.0;
}
// Get text
CFMutableAttributedStringRef attrString =CFAttributedStringCreateMutable(kCFAllocatorDefault,0);
CFAttributedStringReplaceString (attrString,CFRangeMake(0,0),
(CFStringRef) text);
CFIndex stringLength =
CFStringGetLength((CFStringRef) attrString);
// Change font
CTFontRef ctFont =
CTFontCreateWithName((__bridge
CFStringRef) font.fontName, font.pointSize,NULL);
CFAttributedStringSetAttribute(attrString,
CFRangeMake(0, stringLength),
kCTFontAttributeName, ctFont);
// Calc the size
CTFramesetterRef framesetter =CTFramesetterCreateWithAttributedString(attrString);
CFRange fitRange;
CGSize frameSize =
CTFramesetterSuggestFrameSizeWithConstraints(framesetter,
CFRangeMake(0, stringLength),
NULL, CGSizeMake(width,
CGFLOAT_MAX), &fitRange);
CFRelease(ctFont);
CFRelease(framesetter);
CFRelease(attrString);
//NSLog(@"frameSize=======:%f", frameSize.height);
return frameSize.height;
}
@end
@interface StringHeight :
NSObject
+ (CGFloat)heightWithText:(NSString *)text font:(UIFont *)font constrainedToWidth:(CGFloat)width;
@end
#import "StringHeight.h"
#import <CoreText/CoreText.h>
@implementation StringHeight
+ (CGFloat)heightWithText:(NSString *)text font:(UIFont *)font constrainedToWidth:(CGFloat)width
{
if (!text) {
return 0.0;
}
// Get text
CFMutableAttributedStringRef attrString =CFAttributedStringCreateMutable(kCFAllocatorDefault,0);
CFAttributedStringReplaceString (attrString,CFRangeMake(0,0),
(CFStringRef) text);
CFIndex stringLength =
CFStringGetLength((CFStringRef) attrString);
// Change font
CTFontRef ctFont =
CTFontCreateWithName((__bridge
CFStringRef) font.fontName, font.pointSize,NULL);
CFAttributedStringSetAttribute(attrString,
CFRangeMake(0, stringLength),
kCTFontAttributeName, ctFont);
// Calc the size
CTFramesetterRef framesetter =CTFramesetterCreateWithAttributedString(attrString);
CFRange fitRange;
CGSize frameSize =
CTFramesetterSuggestFrameSizeWithConstraints(framesetter,
CFRangeMake(0, stringLength),
NULL, CGSizeMake(width,
CGFLOAT_MAX), &fitRange);
CFRelease(ctFont);
CFRelease(framesetter);
CFRelease(attrString);
//NSLog(@"frameSize=======:%f", frameSize.height);
return frameSize.height;
}
@end
相关文章推荐
- ios手柄研究
- ava垃圾加收机制和ios的arc有什么区别
- iOS button 同时设定图片和文字
- iOS 高效 Mac 配置
- iOS开发 获取应用名称和版本号
- IOS自动布局一
- 通过iOS中的按钮来触发html文件中按钮所触发的函数
- iOS开发 将图片打包编码以便上传
- iOS-登陆界面 实现光标换行功能
- iOS开发 字符串转换成字典
- iOS UTF-8、URL解码与转码
- iOS8新功能 tableviewcell 自适应高度 iPhone5siPhone6 与iPhone6p的差异设置
- 苹果开发 笔记(56)下载zip文件到沙箱更新
- iOS 9 更新之Safari广告拦截器(Content Blocker)开发教程
- iOS多线程编程之NSThread的使用
- iOS—Xcode中设置Framework Search Paths的秘密!
- iOS-代理模式
- 键盘收起
- 结合一个nagios告警浅谈solaris的SVM
- iOS contentOffset和contentInset的区别