问题
Using the storyboard I created a tableview with cells that have labels.
I want to be able to easily assign the same font to all labels in my app so I created some fonts in a helper file (smallFont, mediumFont, and largeFont) and in tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
I set each label to use the font I want (i.e. cell.label.font = Common.fonts.mediumFont).
The first time the tableview displays it is using the font defined in the storyboard and it doesn't use the font I programed in until you navigate away from the view and back.
I can eliminate the visual resizing by changing the storyboard to use the same font that I place programmatically but I want to be able to change the fonts across the entire app just by adjusting the font in the Common file.
How can I override the storyboard's font and force it to use the programmed in font when the view first appears?
Edit: To add some more information.
When I scroll the table the label fonts change from the storyboard size to the programmed fonts.
回答1:
I have fixed it by setting the font in both cellForRowAtIndexPath
and willDisplayCell
methods.
The willDisplayCell
is implemented this way:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell,
forRowAt indexPath: IndexPath) {
// e.g.
let myCell = cell as! MyCustomCell
myCell.label.font = UIFont.boldSystemFont(ofSize: 17.0)
}
Note: that the font is set on the cell
input parameter in the willDisplayCell
method, as shown in the code above. If the cell is taken from the tableView
using the tableView.dequeueReusableCell(...)
it will not work.
回答2:
Did you try modifying the font from awakeFromNib from the cell class?
Here's a sample:
class CustomFontCell: UITableViewCell {
@IBOutlet var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
titleLabel.font = Common().appleBerryFont(size: 28.0)
}
}
struct Common {
func appleBerryFont(size: CGFloat) -> UIFont {
return UIFont(name: "appleberry", size: size)!
}
}
Typically, awakeFromNib is used to perform additional initializations.
回答3:
First, replace your common method by create a font. I cannot think of any cases that this won't work.
label.font = UIFont.systemFontOfSize(20)
Assuming this works, use a break point to debug your original code. You need to make sure that on the first time, your testFont line is executed and with a valid not-nil value.
let testFont = Common.fonts.mediumFont
label.font = testFont
回答4:
Hmm you can try prepareForReuse() instead, this function is use to perform some final modifications or resetting:
override func prepareForReuse() {
super.prepareForReuse()
titleLabel.font = Common().appleBerryFont(size: 28.0)
}
回答5:
Another technique - subclass UILabel and set the font there. Use that label in your storyboard where needed. For example:
import UIKit
class StandardLabel: UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit(){
self.font = Common.fonts.mediumFont
}
}
Then in your storyboards, change your label class to StandardLabel.
回答6:
In your custom Label you should override
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
}
and make Font changes there
来源:https://stackoverflow.com/questions/44783157/uilabels-font-not-adjusting-until-view-reappears