[http://blog.sina.com.cn/s/blog_691a202f0101bq6q.html]
下面主要讲,如何设置字体,间距,并计算(带特定段间距,行间距,字间距,字大小)文字的高度。
1.首先当然是导入CoreText.framework,如果这个不会,下面的你就别看了!
2 自己写一个继承UIView的类,我写的叫 MyView
.h代码如下:
#import
@interface Myview : UIView
@property(nonatomic,retain)NSString *text; //要画的文字
@property(nonatomic,assign)CGFloat font; //字体大小
@property(nonatomic,assign)CGFloat character; //字间距
@property(nonatomic,assign)CGFloat line; //行间距
@property(nonatomic,assign)CGFloat paragraph;//段落间距
@end
看到上面的5个属性,就是我们自己,可以随意设置的。
.m部分的全部代码如下,看不懂没关系,会用就行(东西都是,先了解,会用,然后理解)
#import "Myview.h"
#import
@implementation Myview
-(void)dealloc
{
[_text release];
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.font = 15;
self.text = @"请给myview.text赋值";
self.line = 10;
self.paragraph = 20;
self.character = 4;
}
return self;
}
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
//创建AttributeStringfdsa
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
initWithString:self.text];
//创建字体以及字体大小
CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), self.font, NULL);
CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), self.font, NULL);
//字体,把helvetica 样式加到整个,string上
[string addAttribute:(id)kCTFontAttributeName
value:(id)helvetica
range:NSMakeRange(0, [string length])];
//字体样式 ,把helveticaBold 样式加到整个,string上
[string addAttribute:(id)kCTFontAttributeName
value:(id)helveticaBold
range:NSMakeRange(0, [string length])];
//颜色,此处为黑色,你可以自己改颜色,[UIColor redColor]
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor blackColor].CGColor
range:NSMakeRange(0, [string length])];
//创建文本对齐方式
CTTextAlignment alignment = kCTJustifiedTextAlignment;//对齐方
CTParagraphStyleSetting alignmentStyle;
alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;
alignmentStyle.valueSize=sizeof(alignment);
alignmentStyle.value=&alignment;
//设置字体间距
long number = self.character;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
[string addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(0, [string length])];
CFRelease(num);
//创建文本, 行间距
CGFloat lineSpace=self.line;//间距数据
CTParagraphStyleSetting lineSpaceStyle;
lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;
lineSpaceStyle.valueSize=sizeof(lineSpace);
lineSpaceStyle.value=&lineSpace;
//设置 段落间距
CGFloat paragraph = self.paragraph;
CTParagraphStyleSetting paragraphStyle;
paragraphStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
paragraphStyle.valueSize = sizeof(CGFloat);
paragraphStyle.value = ¶graph;
//创建样式数组
CTParagraphStyleSetting settings[]={
alignmentStyle,lineSpaceStyle,paragraphStyle
};
//设置样式
CTParagraphStyleRef paragraphStyle1 = CTParagraphStyleCreate(settings, sizeof(settings));
//给字符串添加样式attribute
[string addAttribute:(id)kCTParagraphStyleAttributeName
value:(id)paragraphStyle1
range:NSMakeRange(0, [string length])];
// layout master
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
//计算文本绘制size ,这里300是文字宽度,你可以自己更改为247,但是要记得,在height 方法里的这个位置,也改为247
CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(300, MAXFLOAT), NULL);
//创建textBoxSize以设置view的frame
CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height + 1);
// NSLog(@"textBoxSize0 == %f,%f,%f",textBoxSize.width,textBoxSize.height,textBoxSize.width / textBoxSize.height);
self.frame = CGRectMake(0, 0, textBoxSize.width , textBoxSize.height);
[string release];
//- (void)drawRect:(CGRect)rect;代码
CGMutablePathRef leftColumnPath = CGPathCreateMutable();
CGPathAddRect(leftColumnPath, NULL,
CGRectMake(0, 0,
self.bounds.size.width,
self.bounds.size.height));
CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(0, 0),
leftColumnPath, NULL);
// NSLog(@"textBoxSize1 == %f,%f",self.frame.size.width,self.frame.size.height);
// flip the coordinate system
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.frame);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor]CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, 320, self.frame.size.height));
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// draw
CTFrameDraw(leftFrame, context);
// cleanup
CGPathRelease(leftColumnPath);
CFRelease(framesetter);
//CFRelease(helvetica);
// CFRelease(helveticaBold);
UIGraphicsPushContext(context);
}
#pragma mark - 计算高度的方法
//这个方法,剪切到你要用的那个类里面,就ok了
// 或者,就放这里,但改成实例方法,+(CGSize)height:^^^^^^^^^
-(CGSize)height:(NSString *)text Font:(CGFloat)font Character:(CGFloat)character Line:(CGFloat)line Pragraph:(CGFloat)pragraph
{
//创建AttributeStringfdsa
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
initWithString:text];
//创建字体以及字体大小
CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), font, NULL);
CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), font, NULL);
//添加字体目标字符串从下标0开始到字符串结尾
[string addAttribute:(id)kCTFontAttributeName
value:(id)helvetica
range:NSMakeRange(0, [string length])];
//添加字体目标字符串从下标0开始,截止到4个单位的长度
[string addAttribute:(id)kCTFontAttributeName
value:(id)helveticaBold
range:NSMakeRange(0, [string length])];
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor whiteColor].CGColor
range:NSMakeRange(0, [string length])];
CTTextAlignment alignment = kCTJustifiedTextAlignment;//这种对齐方式会自动调整,使左右始终对齐
CTParagraphStyleSetting alignmentStyle;
alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;//指定为对齐属性
alignmentStyle.valueSize=sizeof(alignment);
alignmentStyle.value=&alignment;
//设置字体间距
long number = character;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
[string addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(0, [string length])];
CFRelease(num);
//创建文本行间距
CGFloat lineSpace=line;//间距数据
CTParagraphStyleSetting lineSpaceStyle;
lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;//指定为行间距属性
lineSpaceStyle.valueSize=sizeof(lineSpace);
lineSpaceStyle.value=&lineSpace;
//设置段落间距
CGFloat paragraph = pragraph;
CTParagraphStyleSetting paragraphStyle;
paragraphStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
paragraphStyle.valueSize = sizeof(CGFloat);
paragraphStyle.value = ¶graph;
//创建样式数组
CTParagraphStyleSetting settings[]={
alignmentStyle,lineSpaceStyle,paragraphStyle
};
//设置样式
CTParagraphStyleRef paragraphStyle1 = CTParagraphStyleCreate(settings, sizeof(settings));
//给字符串添加样式attribute
[string addAttribute:(id)kCTParagraphStyleAttributeName
value:(id)paragraphStyle1
range:NSMakeRange(0, [string length])];
// layout master
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
//计算文本绘制size
CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(300, MAXFLOAT), NULL);
//创建textBoxSize以设置view的frame
CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height + 1);
[string release];
return textBoxSize;
}
@end
3. 注意上面的Myview 默认要绘制的文字宽度是 300,如果你想要的文字宽度是 200,请你去改代码,
//计算文本绘制size ,这里300是文字宽度,你可以自己更改为200,但是要记得,在height 方法里的这个位置,也改为200,这个很重要
CGSize tmpSize =CTFramesetterSuggestFrameSizeWithConstraints(framesetter,CFRangeMake(0,0), NULL, CGSizeMake(300, MAXFLOAT), NULL);
把这个MyView 加入到你的工程里面,就完成了工作的80%了,接下来,更简单,
4 看完下面的,你就会了
我们知道,不同文字他的高度,是不确定的,如果超过 height 460,548,那么你一个屏幕是显示不下的
这时候,我们怎么用这个Myview呢
请看下面的具体用法,
@property(nonatomic,assign)CGSize size;
@property(nonatomic,retain)NSString *text;
- (void)viewDidLoad
{
[super viewDidLoad];
self.text = @"";你自己赋值,
self.size = [self height:self.text Font:15 Character:4 Line:10 Pragraph:20]; //必须先计算高度
NSLog(@"width:%f, height:%f",self.size.width,self.size.height);
}
//在下面这个方法中的用法,
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Myview *myview = [[Myview alloc]initWithFrame:CGRectMake(0, 0, self.size.width, self.size.height)];//知道为什么,先算高度了吧
myview.text = self.text;
myview.font = 15;
myview.line = 10;
myview.paragraph = 20;
myview.character = 4; //这些值比须和,height方法里传的5个参数一一对 //应相等。
UIView *v1 = [[UIView alloc]initWithFrame:CGRectMake(8, 10, 304, self.size.height)]; //这里并不是多此一举,不信你自己去掉试试,你会发 //现,你画的文字,是从(0,y,width,height)这里开始画的,和里myview的 //CGRectMake(x,y,300,height)x,y无关。
[v1 addSubview:myview];
[cell addSubview:v1];
当然cell的高度,也是我们算好了的
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.size.height+20;
}
5 到此,你应该已经会了,如果关键的第4步,你没看懂,那么请看下篇,我贴出了,使用Myview的详细全部代码,你直接使用之后就明白了。