大佬教程收集整理的这篇文章主要介绍了swift – 带图标和文本的UITableViewRowAction,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我现在有三个动作的表行:
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] }
但是,我想要的不是我所选择的大小和样式的居中文字的颜色方块,而是:
我尝试使用BACkgroundColor,但只是将图像平铺在按钮上,并没有改变文本的位置或颜色.所以它必须不仅仅是
theAction.BACkgroundColor = UIColor(patternImage: UIImage(named: "renameImage")!)
有这样做的好方法吗?
第一部分是标准委托方法:
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,indexPath) in self.locateEntry(indexPath) } self.fixAction(locate,text: "Locate",image: UIImage(named: "locatePin")!,color: UIColor(38,107,215)) let delete = UITableViewRowAction(style: .normal,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,rename] }
那里有两个不明显的细节.首先,动作从传入的文本字符串中获取其宽度.如果您没有通过一些不可见的空格字符给它一些宽度,则背景图像将不会有任何区域被绘制.这就是为什么前3个操作中使用的stockWidth字符串.它的8个字符宽度由生成背景图像的fixAction方法共享.
第二个细节是在底部包含第四个“间隙”动作.出于某种原因,如果第一个动作具有作为平铺图案的背景颜色,则它将在其他动作下向左伸展.我发现我必须在前面插入这个零宽度没有操作以避免这种情况.
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全部内容,希望文章能够帮你解决swift – 带图标和文本的UITableViewRowAction所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。