UILabel使用attribute string时,显示单行文字时,计算出的高度不太正常,底部会多出行间距(lineSpacing
);多行时,就是正常的。
解决方法: 在attributes字典中,只在文字展示为多行的情况下设置NSParagraphStyleAttributeName
。
NSString *text = @"<#…………#>";
CGFloat labelMaxWidth = DGScreenWidth - 16 - 16;
CGSize maxSize = CGSizeMake(labelMaxWidth, CGFLOAT_MAX);
/*
当文字展示为一行时,如果使用了NSParagraphStyle,底部会出现高度为lineSpacing的空白!!!
为了避免出现这种情况,不使用NSParagraphStyle就可以了
*/
NSMutableDictionary *attrs = @{
NSForegroundColorAttributeName: kColor3D3D3D,
NSFontAttributeName: [UIFont systemFontOfSize:16 weight:UIFontWeightMedium],
}.mutableCopy;
CGFloat singleLineHeight = [@"中" boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.height;
CGFloat totalLineHeight = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.height;
BOOL isSingleLine = ABS(totalLineHeight - singleLineHeight) <= DBL_EPSILON;
if (!isSingleLine) {
NSMutableParagraphStyle *paragraphStyle = [NSParagraphStyle defaultParagraphStyle].mutableCopy;
paragraphStyle.lineSpacing = 10;
attrs[NSParagraphStyleAttributeName] = paragraphStyle;
}
self.textLabel.attributedText = [[NSAttributedString alloc] initWithString:text attributes:attrs];
在Storyboard中使用了静态cell,cell包含auto layout约束,关联了IBOutlet,然后设置其accessoryView
,导致CPU使用率飚升,界面卡死
解决方法: 不使用accessoryView
,直接将对应的视图放在静态cell中,并添加相关的约束
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "XXX" nib but the view outlet was not set.'
解决方法: 在xib
文件中添加一个View
,而不是一个ViewController
。File's Owner
对应的Class
应为自定义视图控制器的Class
控制台警告Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.
解决方法: 如果使用的是xib或storyboard,把backgroudColor
设置为default
就行了。
如果使用的是代码,将设置其backgroundColor
的代码删掉。
如果必须设置backgroundColor
,需要创建一个view,设置该view的backgroundColor
,并将其赋给backgroundView
属性。
UITableView
的tableHeaderView
或tableFooterView
位置不正确(与cell
重叠)
解决方法: 必须在给这两个属性赋值之前设置frame
(设置bounds
是不行的)。
如果是从xib
创建视图,对象假定为xibView
,必须init
另一个视图,对象假定为wrapView
,然后将xibView
添加到wrapView
上,再将wrapView
赋给tableHeaderView
或tableFooterView
。
XXXView *xibView = ViewFromNib(XXXView);
CGSize size = [xibView systemLayoutSizeFittingSize:CGSizeMake(screenWidth, CGFLOAT_MAX)];
xibView.frame = CGRectMake(0, 0, size.width, size.height);
UIView *wrapView = [[UIView alloc] initWithFrame:xibView.bounds];
[wrapView addSubview:xibView];
self.tableView.tableHeaderView = wrapView;
自定义UITableViewHeaderFooterView背景色问题
自定义UITableViewHeaderFooterView
,设置了其contentView
的背景色,在不低于iOS 10
的系统上是有效的,但在iOS 8
、iOS 9
上是透明的。
解决方案:
- (void)awakeFromNib {
[super awakeFromNib];
self.contentView.backgroundColor = DGHexColor(0xf9f9f9);
/*
对于iOS 8、iOS 9,contentView会为nil,
设置其背景色,会没有效果,但由于其 contentView 属性是 readonly 的,
所以新建 backgroundView,并设置其背景色
*/
if (DGIOS8_0_0OrLater && !DGIOS10_0_0OrLater) {
self.backgroundView = [UIView new];
self.backgroundView.backgroundColor = DGHexColor(0xf9f9f9);
}
}
本作品由Daniate采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
评论已关闭