OGeek|极客世界-中国程序员成长平台

标题: ios - UICollectionView 单元格框架具有十进制原值 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:13
标题: ios - UICollectionView 单元格框架具有十进制原值

我有一个以编程方式创建的 UICollectionView。 Collection View 中的每个单元格都显示一个标签,并且所有标签都具有相同的属性。尽管如此,我注意到 Collection View 中心列中每个标签中包含的文本看起来很模糊。

Collection View 的递归描述显示表格中的中心单元格始终有一个 x 原点,它是一个十进制值:

 <UICollectionViewCell: 0xd98c1e0; frame = (242.5 0; 220 45); layer = <CALayer: 0xd98c270>> ...

我的问题是:1)这会导致模糊吗?和 2) 确保 x 和 y 原点都没有最终具有十进制值的最佳方法是什么? (除了手动计算布局)

作为引用,这是我的 UICollectionView 代码:

//subclass UICollectionViewFlowLayout

@implementation LabelLayout

-(BOOL)shouldInvalidateLayoutForBoundsChangeCGRect)newBounds{
    return YES;
}

- (id)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)prepareLayout
{
    [super prepareLayout];
    _cellCount = [[self collectionView] numberOfItemsInSection:0];
}

- (void)setup
{
    self.itemSize = CGSizeMake(220.0f, 45.0f);
    self.sectionInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
    self.minimumLineSpacing = 10.0f;
    self.minimumInteritemSpacing = 20.0f;
}

@end



Best Answer-推荐答案


这肯定会导致模糊,因为所有内容都在两个像素上进行了抗锯齿处理(注意:在视网膜屏幕上,您不会看到任何模糊,因为每个点实际上是两个像素,所以从技术上讲,半个点作为单个像素存在) .要修复它,您可能必须使用 CGRectIntegral 将帧强制为整数边界。最好的方法是:

- (NSArray *)layoutAttributesForElementsInRectCGRect)rect{
    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
    for (NSLayoutAttributes *attribute in attributes){
        attribute.frame = CGRectIntegral(attribute.frame);
    }
}

你应该对 layoutAttributesForItemAtIndexPath: 做同样的事情。

关于ios - UICollectionView 单元格框架具有十进制原值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17957742/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4