美文网首页
iOS导入字体和字体压缩

iOS导入字体和字体压缩

作者: 严谨风 | 来源:发表于2016-08-08 14:16 被阅读739次

前言

由于懒省事我就把导入字体和一个非常奇葩的要求---字体压缩一起写在这篇文章中了。
不过为了读者查阅方便,我会把两部分分开来讲。


导入字体

在某些情况下,我们可能需要使用特殊字体来显示文本内容,如果使用的字体不是iOS系统中预装的字体,那么这个时候我们就需要自己将字体包添加到应用中了。
iOS中字体导入很简单:

  1. 将字体包(ttf文件)添加进工程中。
  2. 在项目的info中添加如下代码:
<key>UIAppFonts</key>
 <array>
  <string>hya3g.ttf</string>
 </array>

其中hya3g.ttf便是字体包的名称。
3.使用字体包。

btn.titleLabel.font = [UIFont fontWithName:@"HYZhongSongJ" size:13];

以上三步,前两步都很简单,第三步却有个坑,不注意的话就会掉进去。那就是字体的名称和字体包资源的名称不同。字体包资源的名称是hya3g.ttf,但是字体的名称却叫HYZhongSongJ,在App中如果想使用字体,需要使用字体的名称来加载,这一点要特别注意。至于如何知道字体的名称,请打开字体包,自行查看。

字体压缩

说到字体压缩,可能很多人都没听过,嗯,当然没听过,因为这个词是我随口胡编的,我也不知道专业名词叫什么。具体意思是什么呢?打个比方,一个14号的字体,大小是固定的,多高,多宽,有标准。而字体压缩就是在这个标准上进行压缩,例如,让字体的高不变,宽缩小到原来的75%!
这就是字体压缩。
这样的需求很奇怪吧?
没见过吧?
我也是第一次见!
所以本着有困难要上,没有困难创造困难也要上的自虐精神,我们就试着来实现一下吧。
对于字体上这么变态的要求,我首先想到了CoreText,但是很遗憾,我没有找到相关的属性。
于是,我想到了Stackoverflow,然后我就找到了这个问答。
http://stackoverflow.com/questions/10063601/ios-uifont-reducing-font-width
在这个问答中,有两个可用的答案。

label.layer.transform = CATransform3DMakeScale(desiredWidth/textWidth, 1.0, 1.0);

这个回答有两个赞同,看上去不错,然后我们可以试一试,发现,哎,真的好用啊!字体被压缩啦!开心。



呵呵,很快发现,布局完全乱套啦,所有的布局都想中间移动啦。
所有更改的地方都要重新布局!至此,我没有在这条路上走的更深,如果有人喜欢这个方法,可以继续走下去,说不定能走出一片新天地。

。。。代码太长啦,我就不贴上啦。不过由于这个问答时间久远,很多方法已经被废弃,而且很多地方的需求和我不同,所以我对这段代码进行了大改!
改完后的代码:

- (void)drawRect:(CGRect)rect; {
    CGRect bounds = self.bounds;
    NSString *text = self.text;
    UIFont *font = self.font;
    
    CGFloat scaleX = 0.75;
    CGFloat scaleY = 1.0;
    
    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    style.alignment = self.textAlignment;
    
    CGRect textRect = [text boundingRectWithSize:CGSizeMake(bounds.size.width / scaleX, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font, NSParagraphStyleAttributeName : style} context:nil];
    
    CGSize textSize = [text boundingRectWithSize:CGSizeMake(bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesFontLeading  attributes:@{NSFontAttributeName : font} context:nil].size;
    
//    CGSize textSize = [self systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];
    
    NSLog(@"text ---- %@, bounsW --- %f, bounsH --- %f", text, bounds.size.width, bounds.size
          .height);
    NSLog(@"text ---- %@, rectW --- %f, rectH --- %f", text, textRect.size.width, textRect.size.height);
    NSLog(@"text ---- %@, sizeW --- %f, sizeH --- %f", text, textSize.width, textSize.height);
    // Find the space needed for all the text.
//    CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName : font}];
    // topLeft is the point from which the text will be drawn.  It may have to move due to compensate for scaling, or due to the chosen alignment.
    CGPoint topLeft = bounds.origin;
    
    // Default to no scaling.
    
//    textRect.size.width *= scaleX;
//    textRect.size.height *= scaleY;
    // Alignment only matters if the label text doesn't already fill the space available.
    switch (self.textAlignment)
    {
        case NSTextAlignmentLeft :
        {
            topLeft.x = bounds.origin.x;
        }
            break;
            
        case NSTextAlignmentCenter :
        {
            NSLog(@"center ----- %@", text);
            topLeft.x = bounds.origin.x + (bounds.size.width - textRect.size.width) / 2;
        }
            break;
            
        case NSTextAlignmentRight :
        {
            topLeft.x = bounds.origin.x+bounds.size.width - textRect.size.width;
        }
            break;
        default:
            break;
    }
    
    // Also adjust the height if necessary.
    NSLog(@"text ---%@, %f, %f", text, round(self.bounds.size.height), round(textRect.size.height) );
    if (round(self.bounds.size.height) != round(textRect.size.height)) {
        topLeft.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2;
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(context, scaleX, scaleY);
    
    // The text color may change with highlighting.
    UIColor *currentTextColor;
    if ((![self isHighlighted]) || (![self highlightedTextColor]))
        currentTextColor = [self textColor];
    else
        currentTextColor = [self highlightedTextColor];
    
//    topLeft.x /= scaleX;
//    topLeft.y /= scaleY;
    if (currentTextColor)
    {
//        if (textSize.height < textRect.size.height) {
//            [text drawInRect:CGRectMake(0, topLeft.y, rect.size.width / scaleX, rect.size.height) withAttributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : currentTextColor, NSParagraphStyleAttributeName : style}];
//        } else {
//            [text drawInRect:CGRectMake(topLeft.x, topLeft.y, rect.size.width / scaleX, rect.size.height) withAttributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : currentTextColor}];
//        }
        
        [text drawInRect:CGRectMake(0, topLeft.y, rect.size.width / scaleX, rect.size.height) withAttributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : currentTextColor, NSParagraphStyleAttributeName : style}];
    }
    
}

由于这段代码量比较大,其中我经历过的修改痕迹也并没有删除,而是注释掉了,所以如果认真读这段代码的话应该可以比较容易理解我的思路的。所以我对这段代码不再详细解释。只说scaleX是字体在X轴方向上的压缩比例,scaleY同理。


如果本文对你的学习有所帮助,请点亮喜欢。如果本文对你没有帮助,无视就好。进来由于简书上的伸手党日渐增多,所以我的文章中用到的各种代码我都会以代码段的形式放出,有需要的可以直接copy。但是本人不提供修改代码适应各种需求的服务,如果有需要,请自行修改。

相关文章

  • iOS导入字体和字体压缩

    前言 由于懒省事我就把导入字体和一个非常奇葩的要求---字体压缩一起写在这篇文章中了。不过为了读者查阅方便,我会把...

  • iOS App导入自定义字体

    原文iOS App导入自定义字体 需求 iOS项目中需要导入第三方字体,来进行显示字体样式。 实现方法 1 先找到...

  • iOS打印导入的字体名称

    在iOS中,使用字体,不是使用字体包的名称,而是需要导入包体在iOS中对应的名称,打印字体名称如下: 对字体的使用

  • iOS开发之导入字体库 2019-07-05

    在我们做iOS开发中,系统的字体有时候满足不了的产品和设计的需求,这时候就需要导入字体库了,导入字体库很容易,但是...

  • iOS字体设置,调用方式不变

    iOS设置字体有两种方式 是导入我们的字体到工程,调用[UIFont fontWithName:fontName ...

  • iOS App导入自定义字体

    需求 iOS项目中需要导入第三方字体,来进行显示字体样式。 实现方法 1 先找到适合的字体 先在字体吧找到漂亮好看...

  • Swift 更改字体

    首先,导入字体: 其次,使用:(注意iOS11之后问题) if #available(iOS 11.0, *) {...

  • UIFont「自定义字体」

    iOS自定义字体 1.将字体文件导入工程(.ttf) 2.打开Build Phases -> Copy Bundl...

  • 不得不说的iOS字体在app中的使用规范

    iOS系统家族字体(family font) 系统字体意味着可以不需要另外导入字体包就可以在项目中使用,而且不涉及...

  • iOS在项目中使用自定义字体

    iOS 自定义字体是指,使用自己从外部导入的字体库使用在对应的文本显示中。 一般的iOS自定义的字体库是 .ttf...

网友评论

      本文标题:iOS导入字体和字体压缩

      本文链接:https://www.haomeiwen.com/subject/dykrsttx.html