Latest 1.0.3
Homepage https://github.com/mydy1987/XOCenterImageTitleButton
License MIT
Platforms ios 8.0, requires ARC
Authors

平常会遇到很多次要把按钮图片放上面,文字放下面,然后文字和图片水平居中的情况,于是写了这个类.
eg

要求

  • iOS 8.0+
  • Swift 4.0+

安装

CocoaPods

Podfile中加入:

pod 'XOCenterImageTitleButton'

手动

把XOCenterImageTitleButton.swift拖入工程.

使用方法

・用代码

这个类完全继承自UIButton,用法与UIButton完成一样,你可以用btn.imageView以及btn.titleLabel进行各种属性的设置。

let btn = XOCenterImageTitleButton.init(frame: CGRect.init(x: 170, y: 200, width: 100, height: 100))
self.view.addSubview(btn)

btn.setImage(UIImage.init(named: "icon"), for: .normal)
btn.setTitle("title", for: .normal)
・用 Storyboard 或者 XIB

在StoryBoard或者XIB中放入一个UIButton,把Class设置为 XOCenterImageTitleButton
via Storyboard

自定义(主要是设置图片与文字的位置,大小)

・用代码
btn.imageViewTopSpaceToButton = 10 // 默认是0
btn.imageViewWidth = 50 // 默认与按钮同宽
btn.imageViewHeight = 50 // 默认是按钮高度的2/3

btn.titleLabelTopSpaceToImageView = 10 // 默认是0
btn.titleLabelWidth = 100 // 默认与按钮同宽
btn.titleLabelHeight = 20 // 默认是按钮高度的1/3

btn.backgroundColorForHighlightedState = .blue // 设置按钮按下时按钮的背景色
btn.imageViewAlphaForHighlightedState = 0.5 // 设置按钮按下时图片的透明度
・用 Storyboard 或者 XIB

在面板里,可以设置这6个属性,实现对imageView titleLabel的位置和大小的控制,但无论怎么设置,imageView和titleLabel都是水平居中的
via Storyboard

This is a button that makes imageView and titleLabel horizontally center.

eg

Requirements

  • iOS 8.0+
  • Swift 4.0+

Installation

CocoaPods

Add the following line to your Podfile:

pod 'XOCenterImageTitleButton'

Manual

Just drag XOCenterImageTitleButton.swift to your project.

How to use

・By coding

It is a subclass of UIButton,so the usage is the same with that of UIButton.

let btn = XOCenterImageTitleButton.init(frame: CGRect.init(x: 170, y: 200, width: 100, height: 100))
self.view.addSubview(btn)

btn.setImage(UIImage.init(named: "icon"), for: .normal)
btn.setTitle("title", for: .normal)
・By using Storyboard or XIB

Add Button object and set Custom Class XOCenterImageTitleButton
via Storyboard

Customize

・By coding
btn.imageViewTopSpaceToButton = 10 // default is 0
btn.imageViewWidth = 50 // default is equal to the width of the button
btn.imageViewHeight = 50 // default is equal to  2/3 of the height of the button

btn.titleLabelTopSpaceToImageView = 10 // default is 0
btn.titleLabelWidth = 100 // default is equal to the width of the button
btn.titleLabelHeight = 20 // default is equal to  1/3 of the height of the button

btn.backgroundColorForHighlightedState = .blue
btn.imageViewAlphaForHighlightedState = 0.5
・By using Storyboard or XIB

via Storyboard

Latest podspec

{
    "name": "XOCenterImageTitleButton",
    "version": "1.0.3",
    "license": "MIT",
    "summary": "An center image title button on iOS.",
    "homepage": "https://github.com/mydy1987/XOCenterImageTitleButton",
    "authors": {
        "u8d75u8fdcu4e1c": "[email protected]"
    },
    "source": {
        "git": "https://github.com/mydy1987/XOCenterImageTitleButton.git",
        "tag": "1.0.3"
    },
    "requires_arc": true,
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "XOCenterImageTitleButton/*.swift",
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "4.0"
    },
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This