简体   繁体   中英

Font Size is either 0 or 1 (swift3)

I'm using the OpenSans font in my project but when I adjust its size to a value greater than 1, its size doesn't change. If I set its size with 0, it disappears. I don't understand why it behaves like that. Can anyone explain that to me?

Here is the code:

let RememberMe = UILabel(frame: CGRect(x: checkbox.frame.maxX + view.frame.width*0.05, y: 0, width: ForgetPasswordcontainer.frame.width*0.3, height: ForgetPasswordcontainer.frame.height/2))
RememberMe.text = "Remember Me"
RememberMe.font = UIFont(name: "OpenSans-Regular", size: 1)
RememberMe.textColor = Colors().blue
RememberMe.baselineAdjustment = .alignCenters
RememberMe.adjustsFontSizeToFitWidth = true
RememberMe.textAlignment = .center
ForgetPasswordcontainer.addSubview(RememberMe)

Its not changing because of this line:

RememberMe.adjustsFontSizeToFitWidth = true

No matter what you set the size to, once you get to that line you are once again resizing the font to fit the label that the text is contained in. Either remove that line or change the frame of the label. Also, since it is a custom font you need to be sure that you have registered it in your Info.plist .

Its working for me. Please check :

RememberMe.font = UIFont(name: "Open Sans", size: 10)

And add TTF file to your target.

hey have a look at below code

  let RememberMe = UILabel(frame: CGRect(x: 10, y: 50, width: self.view.frame.size.width  , height: 100))
        RememberMe.text = "Remember Me"
        //This add you to selected font and of minimum size which will be adjusted to width itself so no .. appears
        RememberMe.font = UIFont(name: "OpenSans-Regular", size: 1000)
        RememberMe.textColor = UIColor.blue
        RememberMe.baselineAdjustment = .alignCenters
        //Need to add this externally if you want to explicitly increase label size
        RememberMe.font = UIFont.systemFont(ofSize: 100)
        RememberMe.adjustsFontSizeToFitWidth = true
        RememberMe.textAlignment = .center
        self.view.addSubview(RememberMe)

Output : 在此处输入图片说明

1) Must see the frame you provide it and thats correct I think as you are taking this label in other subviews

2) need to provide RememberMe.font = UIFont.systemFont(ofSize: 100) this as to give a size to system Label you are adding as to explicitly increase minimum system font size

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM