gpt4 book ai didi

ios - 调整 UIButton 上的背景图像大小

转载 作者:行者123 更新时间:2023-11-30 12:49:44 37 4
gpt4 key购买 nike

我正在尝试添加一个“日历日”图像来包围可点击的 UI 按钮上的一些文本,如下所示:

let button = UIButton()
let X_Offset : CGFloat = (95 * CGFloat(buttonCount) ) + 10
let scrollHeight = scrollView.bounds.height

button.frame = CGRect(x: X_Offset, y: scrollHeight/6, width: 70, height: 60)
let buttonText = event.startTime.toShortDayOfWeekString() + "\n" + event.startTime.toShortDayOfMonthString()
button.titleLabel!.lineBreakMode = .byWordWrapping
button.titleLabel!.textAlignment = .center
button.setTitle(buttonText, for: .normal)
button.tag = i

button.backgroundColor = CompanyColor.Red.color
let image = UIImage(named: "calendarDay")
button.setBackgroundImage(image, for: .normal)
button.titleLabel?.font = UIFont(name: "Roboto", size: 14)
button.layer.cornerRadius = 8
button.clipsToBounds = true

但是图像对文本的侵占有点太多了: encroaching image in uibutton如何使背景图像稍微放大并为文本留出足够的间隙?

最佳答案

我认为您正在寻找的是为UIButton设置imageEdgeInsets。通过设置这些属性,您可以将图像移动到其默认位置之外的位置。你必须尝试一下才能得到你想要的结果。

有两种方法可以做到这一点。一种是使用Interface Builder,另一种是通过编程方式。我想界面构建器是最简单的方法。下图显示了如何为 UIButton 设置这些属性。

Edit these properties to get your desired result

或者尝试像这样以编程方式使用

button.imageEdgeInsets = UIEdgeInsets(top: -10, left: 32, bottom: -10, right: 50)

您可以像这样移动标题

button.titleEdgeInsets = UIEdgeInsets(top: 0, left:0, bottom: 0, right: 20)

注意:如果您有动态大小按钮,则像这样设置 UIEdgeInsets 可能会产生不同的结果。针对不同的屏幕尺寸。始终确保它在所有屏幕尺寸上都符合预期。

关于ios - 调整 UIButton 上的背景图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41150817/

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