UILabel Dynamic Font size keep breaking

被刻印的时光 ゝ 提交于 2019-12-22 16:40:12

问题


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)

        }
    }

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

as well i modified this properties via storyBoard :

Result :

UPDATE :

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

回答1:


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.

For example :

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.

For this issue you should know that how mach content should be shown on that label or for batter use you can use multiline label.

Hope you will get help from the answer.



来源:https://stackoverflow.com/questions/32106433/uilabel-dynamic-font-size-keep-breaking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!