简体   繁体   English

使单比例 SVG 图像资产行在表格列中与 SF 符号行对齐?

[英]Make single scale SVG image asset rows align in table column with SF Symbol rows?

I have a UITableView of UITableViewCells.我有一个 UITableView 的 UITableViewCells。 Using cell.defaultContentConfiguration I set image and attributedText properties of for each cell.使用cell.defaultContentConfiguration我为每个单元格设置imageattributedText文本属性。

Images created from SF Symbols, all have the same.medium scale, and all align to the same left edge in their column.从 SF Symbols 创建的图像都具有相同的中等比例,并且都对齐到它们列中的相同左边缘。

But images I create from the asset catalog, single scale SVG images, align in the table column skewed left of the SF symbols in the rows above and below, regardless of what I do, and it also causes the text next to asset images to skew left of the column 'edge' I want to maintain.但是我从资产目录中创建的图像,单比例 SVG 图像,在表列中对齐上下行中的 SF 符号向左倾斜,无论我做什么,它也会导致资产图像旁边的文本倾斜我要维护的“边缘”列的左侧。

Thus, I'm getting a staggered effect I don't want.因此,我得到了我不想要的交错效果。

The following two UIImage constructors give me different image padding and/or alignments:以下两个 UIImage 构造函数为我提供了不同的图像填充和/或对齐方式:

UIImage(named: ...)       // Asset catalog image
UIImage(systemName: ...)  // SF symbol image

I tried applying this to the asset image, but it doesn't change anything:我尝试将其应用于资产图像,但它没有改变任何东西:

func withAlignmentRectInsets(UIEdgeInsets) -> UIImage

What would be the best approach to making the asset image align with the SF symbol images in the table?使资产图像与表中的 SF 符号图像对齐的最佳方法是什么?

I figured it out.我想到了。

There are alignment properties for UITableViewCell.defaultContentConfiguiration() which returns a UIListContentConfiguration , that contains (among others), the following two properties: UITableViewCell.defaultContentConfiguiration()有 alignment 个属性,它返回一个UIListContentConfiguration ,其中包含(除其他外)以下两个属性:

imageToTextPadding

directionalLayoutMargins

Between those two properties I can adjust the insets and spacing between the image and text.在这两个属性之间,我可以调整图像和文本之间的插图和间距。 Specifically, through the layout margins, the rendered single scale SVG image size can be tweaked by using the margin values to constrain the boundaries the SVG will be forced to scale itself to fit into.具体来说,通过布局边距,渲染的单个比例 SVG 图像大小可以通过使用边距值来限制 SVG 将被迫缩放自身以适应的边界来调整。

All that was needed was to pass a flag or values to UITableView 's delegate method cellForRowAt() via the data model (eg table view row content data provided through my data source), wherein each row has a flag that indicates whether or not I need to do anything using UIListContentConfiguration properties or not, to deal with the inherent differences in the image types rendered, ie, SF Symbol vs. Asset Catalog.所需要的只是通过数据 model 将一个标志或值传递给UITableView的委托方法cellForRowAt() (例如,通过我的数据源提供的表格视图行内容数据),其中每一行都有一个标志,指示我是否需要使用或不使用UIListContentConfiguration属性做任何事情,以处理呈现的图像类型的内在差异,即 SF Symbol 与 Asset Catalog。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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