UILabel 动态字体大小不断突破 [英] UILabel Dynamic Font size keep breaking

查看:36
本文介绍了UILabel 动态字体大小不断突破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有只有 UILabel 的 CollectionViewCell(与单元格边界相同).我正在获取 fontNames 数组作为 CollectionView 数据源:

I Have CollectionViewCell that has only UILabel(Same bounds as cell). I'm fetching array of fontNames as the CollectionView DataSource :

 func printFonts() {
        let fontFamilyNames = UIFont.familyNames()
        for familyName in fontFamilyNames {
            let names = UIFont.fontNamesForFamilyName(familyName as! String)
            fontNames.append(familyName as! String)

        }
    }

我试图在 UILabel 内的 one 行中显示此字体名称,它在某些情况下有效,但在某些情况下无效,我不知道为什么.这就是我在 cellForItemAtIndexPath 中调整"字体的方式:

I'm trying to display this font names in one line inside the UILabel , it works in some of the cases but in some not, and i have not idea why. this is how i "adjust" the font inside cellForItemAtIndexPath :

   cell.lbl_font.adjustsFontSizeToFitWidth = true
   cell.lbl_font.numberOfLines = 1
   cell.lbl_font.minimumScaleFactor = 0.1
   cell.lbl_font.baselineAdjustment = .AlignCenters
   cell.lbl_font.textAlignment  = NSTextAlignment.Center

我也通过 storyBoard 修改了这个属性:

as well i modified this properties via storyBoard :

结果:

更新:

如何将文本应用于单元格.其中 lbl_font 是单元格内的标签,cellForItemAtIndexPath

How do i apply text to the cell. where lbl_font is the label inside the cell, Inside cellForItemAtIndexPath

  let cell : textCell = collectionView.dequeueReusableCellWithReuseIdentifier("text_cell", forIndexPath: indexPath) as! textCell
       cell.lbl_font.text = fontNames[indexPath.row]
      cell.lbl_font.font = UIFont(name:fontNames[indexPath.row], size:    cell.lbl_font.font.pointSize)
      cell.lbl_font.adjustsFontSizeToFitWidth = true
      cell.lbl_font.numberOfLines = 1
      cell.lbl_font.minimumScaleFactor = 0.1
      cell.lbl_font.baselineAdjustment = .AlignCenters
      cell.lbl_font.textAlignment  = NSTextAlignment.Center
      return cell

推荐答案

有最小字体缩小属性可用.它将字体大小缩小为与当前点大小的最小比例.如果内容变得超过字体的比例,那么它会在末尾挑衅地显示截断点(...).

There is minimum font shrink property is available. it shrinks the font size as minimum ratio with the current point size. If the content become more then the ratio of fonts then it will defiantly shows the truncation dots(...) at the end.

例如:

这里我设置了最小字体比例为 0.4 的属性.

Here I am setting up the property with minimum font scale is 0.4.

在这里您可以看到应用了相同属性的不同文本.

And here you can see different texts applied with the same property.

在第一个和第二个标签中将调整字体大小,因为内容较少,并且按照最小字体比例进行调整.但是在第三个最小字体比例和标签的内容不匹配时,它将显示截断的点(...).

In first and second label will adjust the font size because content is less and it adjust as per minimum font scale ratio. but in 3rd the minimum font ratio and the content of the label is does not matched then it will show the dots(...) with truncation.

对于这个问题,您应该知道该标签上应该如何显示内容,或者对于连击使用,您可以使用多行标签.

希望你能从答案中得到帮助.

Hope you will get help from the answer.

这篇关于UILabel 动态字体大小不断突破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆