ios7新特性--9

介绍
iOS7引入了新的文本渲染框架,叫做TextKit。TextKit是基于抢塔的CoreText渲染引擎的,所有苹果提供的基于文本的控件现在都已经使用TextKit引擎。TextKit是iOS中非常重要的提升,而其中就包括Dynamic Type和font descriptor。 
本文的代码可以在这里下载
github.com/ShinobiControls/iOS7-day-by-day。

Dynamic Type
Dynamic Type是指用户可以更改程序上文字显示。这里不仅仅是指字体的大小,还可以调整字间距以及行间距等。为了达到这个目的,你不能在指定文本控件使用的字体名了,比如指定Helvetica 11pt是不行的,通常你可以设置它为body类型。这个很类似HTML中字体使用方式。 我们来看一下以下代码:
self.subHeadingLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]; 
iOS7中有6中不同的文本风格可选:
•        UIFontTextStyleHeadline
•        UIFontTextStyleBody
•        UIFontTextStyleSubheadline
•        UIFontTextStyleFootnote
•        UIFontTextStyleCaption1
•        UIFontTextStyleCaption2
除了代码可以这是字体,也可以在interface builder中设置:
1.jpg 
如果和autolayout一起使用,dynamic type对用户来说意味着他可以改变你程序的文本显示。在手机Setting设置中可以改变Text Size:
1.png 
这里有7中不同的字体尺寸,以下是其中几个:
dynamic_type_medium.png 
dynamic_type_medium.png 
dynamic_type_large.png 

Font Descriptors
TextKit中引入的另一个概念是字体描述符。比如,我们有一段文字,我们希望它和我们body的字体是一样的,但是需要粗体显示。在之前的iOS版本中,我们可能需要先知道body中使用的字体是什么,然后找到它的粗体,然后利用fontWithName:size:创建一个font对象。
这样不是很直观,而且引入了dynamic type后,我们常常不知道我们正在使用的是什么字体。字体描述符让这一切变得简单起来。
我们可以这样来做:
UIFontDescriptor *bodyFontDesciptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody]; UIFontDescriptor *boldBodyFontDescriptor = [bodyFontDesciptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold]; self.boldBodyTextLabel.font = [UIFont fontWithDescriptor:boldBodyFontDescriptor size:0.0]; 
首先,我们获取body的文本风格,然后调用 fontDescriptorWithSymbolicTraits: 用来设置字体属性。然后可以利用UIFont的fontWithDescriptor:size: 方法获得需要的字体。

修改 UIFontDescriptor的字体还可以修改为以下值:
•        UIFontDescriptorTraitItalic
•        UIFontDescriptorTraitExpanded
•        UIFontDescriptorTraitCondensed
除了可以修改字体描述符,还可以通过一系列属性找到适合的字体,例如:
UIFontDescriptor *scriptFontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:                                                       @{UIFontDescriptorFamilyAttribute: @"Zapfino",                                                         UIFontDescriptorSizeAttribute: @15.0}                                           ]; self.scriptTextLabel.font = [UIFont fontWithDescriptor:scriptFontDescriptor size:0.0]; 
这里,我们指定了font family和大小。其他可以设置的属性包括:
•        UIFontDescriptorNameAttribute
•        UIFontDescriptorTextStyleAttribute
•        UIFontDescriptorVisbileNameAttribute
•        UIFontDescriptorMatrixAttribute
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值