故障与UITableViewHeaderFooterView
这里改变字体大小的问题,故障与UITableViewHeaderFooterView
我子类UITableViewHeaderFooterView并要更改字体大小:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.textLabel.textColor = [UIColor colorWithWhite:0.8 alpha:1.0];
//the font is not working
self.textLabel.font = [UIFont systemFontOfSize:20];
NSLog(@"aaa%@", self.textLabel.font);
}
return self;
}
颜色的东西工作正常,但字体没” T改变,所以我日志出队:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UITableViewHeader *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:MWDrawerHeaderReuseIdentifier];
headerView.textLabel.text = self.sectionTitles[@(section)];
NSLog(@"bbb%@", headerView.textLabel.font);
return headerView;
}
字体还行就在这里,所以我登录didLayoutsubviews
:
-(void)viewDidLayoutSubviews
{
UITableViewHeaderFooterView *head = [self.tableView headerViewForSection:0];
NSLog(@"ccc%@", head.textLabel.font);
}
和字体大小奇迹般地改变回默认!!!但是我之间没有做任何事情,如果我在viewDidLayoutSubviews
中再次更改字体大小,字体就变得正确了。
它驱使我疯狂!
而且,我在子类化单元时做了相同的字体更改,并且它工作正常!所以谁能告诉我发生了什么事?谢谢!
这里是日志:
2014-02-09 16:02:03.339 InternWell[33359:70b] aaa<UICTFont: 0x8da4290> font-family: ".HelveticaNeueInterface-M3"; font-weight: normal; font-style: normal; font-size: 20.00pt
2014-02-09 16:02:03.339 InternWell[33359:70b] bbb<UICTFont: 0x8da4290> font-family: ".HelveticaNeueInterface-M3"; font-weight: normal; font-style: normal; font-size: 20.00pt
2014-02-09 16:02:03.341 InternWell[33359:70b] aaa<UICTFont: 0x8da4290> font-family: ".HelveticaNeueInterface-M3"; font-weight: normal; font-style: normal; font-size: 20.00pt
2014-02-09 16:02:03.342 InternWell[33359:70b] bbb<UICTFont: 0x8da4290> font-family: ".HelveticaNeueInterface-M3"; font-weight: normal; font-style: normal; font-size: 20.00pt
2014-02-09 16:02:03.343 InternWell[33359:70b] ccc<UICTFont: 0x8d22650> font-family: ".HelveticaNeueInterface-MediumP4"; font-weight: bold; font-style: normal; font-size: 14.00pt
2014-02-09 16:02:03.343 InternWell[33359:70b] ccc<UICTFont: 0x8d22650> font-family: ".HelveticaNeueInterface-MediumP4"; font-weight: bold; font-style: normal; font-size: 14.00pt
似乎把它放在正确的地方它不会,但你可以在你的UITableViewHeaderFooterView
子类的layoutSubviews
方法更改字体,它会正确地应用。
- (void)layoutSubviews {
[super layoutSubviews];
// Font
self.textLabel.font = [UIFont systemFontOfSize:20];
}
'UITableViewHeaderFooterView'在'-layoutSubviews'中做调皮的事情,包括改变'textLabel'的颜色。如果它改变字体,我也不会感到惊讶。这也是我得到的解决方法,但它看起来像一个错误。 –
必须有更好的方法来做到这一点 - 'layoutSubviews'应该只包含布局,它可以在单个渲染循环中多次调用,因此,不建议在布局之外放置其他任何东西;我认为更好的方法是创建自定义标题标签,或更新'willDisplayHeaderView'中的字体。 – Zorayr
您可以实现的tableView:willDisplayHeaderView和更改字体实物这样:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if(section == ...)
{
UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
NSAttributedString *headerText = ... ;
headerView.textLabel.attributedText = headerText;
}
}
比选定的答案更好的解决方案。我只是在Swift中做到了,效果很好(并且不需要子类):\t覆盖func tableView(tableView:UITableView,willDisplayHeaderView view:UIView,forSection section:Int){ \t \t if let header = view as? UITableViewHeaderFooterView { \t \t \t header.textLabel.font = UIFont.boldSystemFontOfSize(22.0) \t \t} \t} –
这是一个在斯威夫特一个解决方案 - 这里的想法是,我们有一个UITableViewHeaderFooterView
的子类别叫做SNStockPickerTableHeaderView
;它公开了一种名为configureTextLabel()
的方法,该方法在调用时设置文本标签的字体和颜色。我们只在标题被设置后调用这个方法,即从willDisplayHeaderView
开始,并且字体被正确设置。
// MARK: UITableViewDelegate
func tableView(tableView:UITableView, willDisplayHeaderView view:UIView, forSection section:Int) {
if let headerView:SNStockPickerTableHeaderView = view as? SNStockPickerTableHeaderView {
headerView.configureTextLabel()
}
}
func tableView(tableView:UITableView, viewForHeaderInSection section:Int) -> UIView? {
var headerView:SNStockPickerTableHeaderView? = tableView.dequeueReusableHeaderFooterViewWithIdentifier(kSNStockPickerTableHeaderViewReuseIdentifier) as? SNStockPickerTableHeaderView
if (headerView == nil) {
headerView = SNStockPickerTableHeaderView(backgroundColor:backgroundColor,
textColor:primaryTextColor,
lineSeparatorColor:primaryTextColor)
}
return headerView!
}
这里是习俗UITableViewHeaderFooterView
:
import Foundation
import UIKit
private let kSNStockPickerTableHeaderViewLineSeparatorHeight:CGFloat = 0.5
private let kSNStockPickerTableHeaderViewTitleFont = UIFont(name:"HelveticaNeue-Light", size:12)
let kSNStockPickerTableHeaderViewReuseIdentifier:String = "stock_picker_table_view_header_reuse_identifier"
class SNStockPickerTableHeaderView: UITableViewHeaderFooterView {
private var lineSeparatorView:UIView?
private var textColor:UIColor?
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// We must implement this, since the designated init of the parent class
// calls this by default!
override init(frame:CGRect) {
super.init(frame:frame)
}
init(backgroundColor:UIColor, textColor:UIColor, lineSeparatorColor:UIColor) {
super.init(reuseIdentifier:kSNStockPickerTableHeaderViewReuseIdentifier)
contentView.backgroundColor = backgroundColor
self.textColor = textColor
addLineSeparator(textColor)
}
// MARK: Layout
override func layoutSubviews() {
super.layoutSubviews()
let lineSeparatorViewY = CGRectGetHeight(self.bounds) - kSNStockPickerTableHeaderViewLineSeparatorHeight
lineSeparatorView!.frame = CGRectMake(0,
lineSeparatorViewY,
CGRectGetWidth(self.bounds),
kSNStockPickerTableHeaderViewLineSeparatorHeight)
}
// MARK: Public API
func configureTextLabel() {
textLabel.textColor = textColor
textLabel.font = kSNStockPickerTableHeaderViewTitleFont
}
// MARK: Private
func addLineSeparator(lineSeparatorColor:UIColor) {
lineSeparatorView = UIView(frame:CGRectZero)
lineSeparatorView!.backgroundColor = lineSeparatorColor
contentView.addSubview(lineSeparatorView!)
}
}
下面是结果,请参见节头, “热门股”:
好像textLabel
font
甚至在IOS 11.
另一种解决方案(SWIFT 4)将被忽略是使用自定义标签。如果您已有UITableViewHeaderFooterView
子类,则不应该很困难。
class MyTableViewHeader: UITableViewHeaderFooterView {
let customLabel = UILabel()
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override func layoutSubviews() {
super.layoutSubviews()
customLabel.frame = contentView.bounds
}
func setup() {
customLabel.textColor = UIColor(white: 0.8, alpha: 1)
customLabel.font = UIFont.systemFont(ofSize: 20)
contentView.addSubview(customLabel)
}
}
即使没有子类化,也会出现同样的问题UITableViewHeaderFooterView设置的字体在显示视图时被重置。检查iOS 9.3。 – DevGansta