//
// Button+Extension.swift
// Shop
//
// Created by fox on 2020/11/25.
// Copyright © 2020 Aos. All rights reserved.
//
import Foundation
import UIKit
import Foundation
/// 自定义button
enum ButtonEdgeInsetsStyle {
case ButtonEdgeInsetsStyleTop // image在上,label在下
case ButtonEdgeInsetsStyleLeft // image在左,label在右
case ButtonEdgeInsetsStyleBottom // image在下,label在上
case ButtonEdgeInsetsStyleRight // image在右,label在左
}
extension UIButton {
func layoutButtonEdgeInsets(style:ButtonEdgeInsetsStyle,space:CGFloat) {
var labelWidth : CGFloat = 0.0
var labelHeight : CGFloat = 0.0
var imageEdgeInset = UIEdgeInsets.zero
var labelEdgeInset = UIEdgeInsets.zero
let imageWith = self.imageView?.frame.size.width
let imageHeight = self.imageView?.frame.size.height
let vue = (UIDevice.current.systemVersion) as NSString
if vue.doubleValue >= 8.0 {
labelWidth = (self.titleLabel?.intrinsicContentSize.width)!
labelHeight = (self.titleLabel?.intrinsicContentSize.height)!
}else {
labelWidth = (self.titleLabel?.frame.size.width)!
labelHeight = (self.titleLabel?.frame.size.height)!
}
switch style {
case .ButtonEdgeInsetsStyleTop:
imageEdgeInset = UIEdgeInsets(top: -labelHeight-space/2.0, left: 0, bottom: 0, right: -labelWidth)
labelEdgeInset = UIEdgeInsets(top: 0, left: -imageWith!, bottom: -imageHeight!-space/2.0, right: 0)
case .ButtonEdgeInsetsStyleLeft:
imageEdgeInset = UIEdgeInsets(top: 0, left: -space/2.0, bottom: 0, right: space/2.0);
labelEdgeInset = UIEdgeInsets(top: 0, left: space/2.0, bottom: 0, right: -space/2.0);
case .ButtonEdgeInsetsStyleBottom:
imageEdgeInset = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight-space/2.0, right: -labelWidth)
labelEdgeInset = UIEdgeInsets(top: -imageHeight!-space/2.0, left: -imageWith!, bottom: 0, right: 0)
case .ButtonEdgeInsetsStyleRight:
// To Do print("坐标是====\(labelWidth)=====\(space)")
imageEdgeInset = UIEdgeInsets(top: 0, left: labelWidth+space/2.0, bottom: 0, right: -labelWidth-space/2.0)
labelEdgeInset = UIEdgeInsets(top: 0, left: -imageWith!-space/2.0, bottom: 0, right: imageWith!+space/2.0)
}
self.titleEdgeInsets = labelEdgeInset
self.imageEdgeInsets = imageEdgeInset
}
}
iOS开发swift之设置button文字跟图片排列任意对齐方式
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 在使用UIButton的时候正常设置文字和图片之后 ,图片和文字的位置默认的是并排水平居中button 这时需要使...
- 在使用UIButton的时候正常设置文字和图片之后 ,图片和文字的位置默认的是并排水平居中button 这时需要使...
- 转自:https://github.com/Phelthas/Demo_ButtonImageTitleEdgeI...