UILabel Dynamic Font size keep breaking

2019-05-29 15:46发布

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 :

desc

Result :

desc

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条回答
爷的心禁止访问
2楼-- · 2019-05-29 16:31

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.

enter image description here

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

enter image description here

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.

查看更多
登录 后发表回答