gpt4 book ai didi

swift - 带有图标和文本的 UITableViewRowAction

转载 作者:搜寻专家 更新时间:2023-10-30 21:55:55 26 4
gpt4 key购买 nike

那里有几个类似的问题,但我认为应该有一个最新的 iOS 10 答案,使用 Swift3,不使用私有(private) API,也不依赖于你限制你的图标到 unicode 表情符号。

我现在有包含三个操作的表格行:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let rename = UITableViewRowAction(style: .normal, title: "Rename") { (_, indexPath) in
self.renameEntry(indexPath)
}
let locate = UITableViewRowAction(style: .normal, title: "Locate") { (_, indexPath) in
self.locateEntry(indexPath)
}
locate.backgroundEffect = UIVisualEffect()
let delete = UITableViewRowAction(style: .default, title: "Forget") { (_, indexPath) in
self.deleteEntry(indexPath)
}
return [delete, locate, rename]
}

enter image description here

但是我想要的不是我选择的大小和样式居中的颜色正方形,而是:

enter image description here

我尝试使用 backgroundColor,但这只是将我的图像平铺在整个按钮上,并没有改变文本的位置或颜色。所以它必须不仅仅是一些东西

theAction.backgroundColor = UIColor(patternImage: UIImage(named: "renameImage")!) 

有什么好的方法吗?

最佳答案

我最终做的是动态生成图像作为背景。这需要使用一两个 hack/clever-trick。

第一部分是标准委托(delegate)方法:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

let stockWidth = String(repeating: " ", count: 8)
let rename = UITableViewRowAction(style: .normal, title: stockWidth) { (_, indexPath) in
self.renameEntry(indexPath)
}
self.fixAction(rename, text: "Rename", image: UIImage(named: "pencilEdit")!, color: UIColor(52, 61, 70))
let locate = UITableViewRowAction(style: .normal, title: stockWidth) { (_, indexPath) in
self.locateEntry(indexPath)
}
self.fixAction(locate, text: "Locate", image: UIImage(named: "locatePin")!, color: UIColor(38, 107, 215))
let delete = UITableViewRowAction(style: .normal, title: stockWidth) { (_, indexPath) in
self.deleteEntry(indexPath)
}
self.fixAction(delete, text: "Forget", image: UIImage(named: "triggerDeleteSelector")!, color: UIColor(227, 34, 60))
let gap = UITableViewRowAction(style: .normal, title: "") { (_, _) in
// pass
}
gap.backgroundColor = UIColor.clear
return [gap, delete, locate, rename]
}

那里有两个不明显的细节。首先,该操作从传入的文本字符串中得出其宽度。如果您没有通过一些不可见的空格字符为其提供一些宽度,则背景图像将没有任何可绘制的区域。这就是stockWidth 前 3 个操作中使用的字符串。它的 8 个字符宽度由生成背景图像的 fixAction 方法共享。

第二个细节是在底部包含第四个“间隙” Action 。出于某种原因,如果第一个 Action 有一个瓷砖图案的背景涂料,它会在其他 Action 下向左拉伸(stretch)。我发现我必须在前面插入这个零宽度无操作操作来避免这种情况。

func fixAction(_ action:UITableViewRowAction, text:String, image:UIImage, color:UIColor) {
// make sure the image is a mask that we can color with the passed color
let mask = image.withRenderingMode(.alwaysTemplate)
// compute the anticipated width of that non empty string
let stockSize = action.title!.sizeWithAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 18)])
// I know my row height
let height:CGFloat = 70
// Standard action width computation seems to add 15px on either side of the text
let width = (stockSize.width + 30).ceiling
let actionSize = CGSize(width: width, height: height)
// lets draw an image of actionSize
UIGraphicsBeginImageContextWithOptions(actionSize, false, 0.0)
if let context = UIGraphicsGetCurrentContext() {
context.clear(CGRect(origin: .zero, size: actionSize))
}
color.set()
let attributes = [NSForegroundColorAttributeName: color, NSFontAttributeName: UIFont(name: "Avenir-Book", size: 13)]
let textSize = text.size(attributes: attributes)
// implementation of `half` extension left up to the student
let textPoint = CGPoint(x: (width - textSize.width).half, y: (height - (textSize.height * 3)).half + (textSize.height * 2))
text.draw(at: textPoint, withAttributes: attributes)
let maskHeight = textSize.height * 2
let maskRect = CGRect(x: (width - maskHeight).half, y: textPoint.y - maskHeight, width: maskHeight, height: maskHeight)
mask.draw(in: maskRect)
if let result = UIGraphicsGetImageFromCurrentImageContext() {
// adjust the passed in action's backgroundColor to a patternImage
action.backgroundColor = UIColor(patternImage: result)
}
else {
"WTH!!!".logError()
}
UIGraphicsEndImageContext()
}

关于swift - 带有图标和文本的 UITableViewRowAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41705740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com