UIButton 设置图片在右的几种方法

Share

UIButton的默认布局是图片在左,标题在右。想要得到一个图片在右,标题在做的UIButton,可以使用以下几种方法

方法一: 改变 Button 的语义属性

不同国家的文字书写方向不一样。在中国,文字书写方式是从左往右,因此UIButton的默认布局是图片在左;而在一些文字书写方向是从右往左的国家里,UIButton的默认布局是图片在右。UIView 有一个 semanticContentAttribute 属性,用来控制 View 的布局。我们可以将其强制更改为从右往左,这样 UIButton 的图片就在右边了:

btn.semanticContentAttribute = .forceRightToLeft

方法二: 写一个 UIButton 的子类,重写绘制方法

final class MyCustomTitleImageButton: UIButton {

    /// 是否把图片放在左边,默认关闭
    var isHeadBtn = false

    /// 是否显示箭头
    var needArrow = true

    var itemMaxWidth: CGFloat = 0

    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        if isHeadBtn {
            return super.titleRect(forContentRect: contentRect)
        } else {
            let superRect = super.titleRect(forContentRect: contentRect)
            var width = superRect.size.width
            let maxWidth = self.itemMaxWidth - (self.needArrow ? 16 : 0)
            if width > maxWidth {
                width = maxWidth
            }
            return CGRect(x: 0, y: frame.size.height / 2 - superRect.size.height / 2, width: width, height: superRect.size.height)
        }
    }

    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        if isHeadBtn {
            return super.imageRect(forContentRect: contentRect)
        } else {
            let superRect = super.imageRect(forContentRect: contentRect)
            return CGRect(x: frame.size.width - 18, y: frame.size.height / 2 - superRect.size.height / 2, width: superRect.size.width, height: superRect.size.height)
        }
    }
}

方法三: 利用 iOS 15.0 的新特性

上面的方法二在iOS 15之后的版本就要被废弃了。这时候可以利用iOS 15的新特性 UIButton.Configuration

var config = UIButton.Configuration.plain()
config.imagePlacement = .trailing	// 关键在这里
config.title = "test"
config.titleAlignment = .center
config.image = UIImage(named: "icon_arrow")
let btn2 = UIButton(configuration: config, primaryAction: nil)

Read more

在 AI 时代,做 iOS 逆向分析有哪些高效手段

在 AI 时代,做 iOS 逆向分析有哪些高效手段

AI 发展如此迅猛,传统的代码手艺活已经被 AI 取代了很大一部分。除了生成代码外,AI 在其他的研发流程也能提升很多效率,例如这次要说的 AI 逆向分析。 在手工时代,我们逆向分析一个 iOS app,通常是使用 Hopper 或者 IDA 等软件查看解密的 Mach-O,然后利用软件提供的能力加上人工去分析逻辑流程。这个过程还要求我们懂一些基础的汇编语法。有时候还要去猜测哪些类哪些方法是负责某个逻辑,靠试验去验证。 现在来到了大模型时代,AI 不仅能自己通过一些简单的工具去分析 ipa 和 Mach-O ,也能使用更准更稳的 MCP、Skill 工具来做深度的分析。 Hopper MCP Hopper 在新版本支持了 MCP Server。 把 framework、Mach-O 或者解密后的 IPA 直接丢给它,

By Gray
Cinder:基于 Swift Macro 与 Mach-O Section 的解耦注册机制

Cinder:基于 Swift Macro 与 Mach-O Section 的解耦注册机制

Cinder(Github) 是一个无需逐模块启动注册、支持字符串、类型和函数调用的 Swift 编译期注册框架。 Cinder 是什么 在 iOS app 里借助 Mach-O 注册能力做一些二进制的插桩操作并不罕见。以往这些操作大都使用 objc runtime ,借助 __attribute__((used)) 和 __attribute__((section("name"))) 实现。在 Swift 上实现有一些难度,因为 Swift runtime 能力弱,以及早期的版本没有类似 objc 的 __attribute__ 能力。 * 在 Swift 5.10 版本,Apple 提供了 @_used 和

By Gray