CoreText 轻松设置字体大小,间距,行间距,段间距,算高度
[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的详细全部代码,你直接使用之后就明白了。
CoreText 轻松设置字体大小,间距,行间距,段间距,算高度相关推荐
- pdfptable 设置行间距_[转载]CoreText 轻松设置字体大小,间距,行间距,段间距,算高度...
#import "Myview.h" #import @implementation Myview -(void)dealloc { [_text release]; [super ...
- idea html设置字体大小,intellij idea设置(字体大小、背景)
1. 配置信息说明 Intellij Idea: 2017.2.5 2.具体设置 <1> 设置主题背景.字体大小 File---->Settings----->Appearan ...
- Label设置行间距,段间距,字间距
Label设置行间距,段间距,字间距 使用NSMutableAttributedString设置label属性 直接上代码 - (void)viewDidLoad {[super viewDidLoa ...
- python使用matplotlib画图,绘制三维、二维曲线。设置字体大小以及坐标系间距等
话不多说,直接看代码和效果(不设置字体大小.逐个设置以及批量设置),其中,曲线的大小.类型以及颜色可以看我的另一篇博客. import matplotlib.pyplot as plt from mp ...
- Latex 中设置 表格字体大小,行间距 等
Latex设置表格字体大小格式为: \begin{table}[h] \small %此处写字体大小控制命令 \begin{tabular} \end{tabular}\end{table} Late ...
- android+设置字体行高,android textview设置字体的行距和字间距
android textview设置字体的行距和字间距 字间距 textView有一个属性android:textScaleX是调节字间距的,它的值是一个float型.查看源代码,默认textView ...
- UC浏览器电脑版怎么设置字体大小 UC浏览器字体设置教程
对于一些视力不好的小伙伴进行浏览网页的时候,要是浏览器的字体更大些的话看着就会轻松多了,下面小编就为你带来UC浏览器字体设置教程. UC浏览器电脑版怎么设置字体大小: 打开UC浏览器,点击" ...
- 【最全IDEA个性化教程】idea设置主题+恢复主题默认设置+设置选中代码颜色+关键字颜色+设置字体大小、样式、颜色+设置背景颜色、图片+设置导航栏背景颜色+设置控制台字体样式及背景+常用快捷键)
目录 下载设置主题样式+恢复主题默认设置 1 个性化代码段 1.1设置颜色 设置光标颜色 自定义图片做背景 修改代码段的颜色和背景颜色 选中代码块颜色修改,修改括号颜色 1.2 设置字体大小.格式 1 ...
- vscode中设置字体大小_vscode配置使用教程
vscode设置成中文 vscode默认的语言是英文,对于英文不好的小伙伴可能不太友好.简单几步教大家如何将vscode设置成中文. 按快捷键"Ctrl+Shift+P". 在&q ...
最新文章
- JSP Tomcat8.0运行连接池时发生异常【AbstractMethodError oracle.jdbc.driver.T4CConnection.isValid(I)Z】...
- opencv亚像素点检测
- python 冷门_csvkit---python一个牛逼到不行的csv处理库
- mysql count if语句_COUNT分组条件去重的sql统计语句示例(mysql)
- 如何在 Spring 生态中玩转 RocketMQ?
- h3c防火墙u200配置命令_网络设备配置——H3C命令行基本操作【分级】
- CodeBlocks 汉化教程及多语言
- php rsa加密实例,关于PHP语言的RSA加密实例讲解
- 微软开源PowerShell并支持Linux
- 搭建nginx流媒体服务器(支持HLS)
- QQ音乐下载的flac文件转码mp3文件
- PROFINET非周期读写分析笔记
- 引用与取地址符的区别
- M. Bottle Arrangements
- easyExcel导出表格及合并单元格
- JAVA毕业设计高校人事管理系统计算机源码+lw文档+系统+调试部署+数据库
- 学大伟业:2019年数学竞赛学习经验分享
- python修改yaml文件_Python读取yaml文件的详细教程
- CentOS 6.3下Samba服务器的安装与配置
- 【OCR】AspriseOCR 条码识别 C++