UIButton image位置的调节

这篇博客主要介绍了如何在iOS中使用Objective-C和Swift调整UIButton的图像位置。内容涵盖通过imageEdgeInsets和titleEdgeInsets属性来实现图像居左、居右、居上和居下,并提到在Swift中如果没有设置frame,需要通过Snapkit或Masonry来设定大小或布局后设置图像位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UIButton image位置的调节

调节image的位置主要是通过 UIButton的imageEdgeInsets和titleEdgeInsets这两个属性来实现的

    CGFloat imageWidth = self.imageView.bounds.size.width;
    CGFloat imageHeight = self.imageView.bounds.size.height;
    CGFloat titleWidth = self.titleLabel.bounds.size.width;
    CGFloat titleHeight = self.titleLabel.bounds.size.height;
    CGFloat insetAmount = space / 2;
    CGFloat width = self.frame.size.width;
  1. 图片居左,调整 image 和 文字之间的间距 (space 是两者之间的间距)
   self.imageEdgeInsets = UIEdgeInsetsMake(0,-insetAmount, 0, insetAmount);
   self.titleEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, -insetAmount);
  1. 图片居右,调整 image 和 文字之间的间距
  self.imageEdgeInsets = UIEdgeInsetsMake(0,(titleWidth + insetAmount), 0, -(titleWidth + insetAmount));
  self.titleEdgeInsets = UIEdgeInsetsMake(0,-(imageWidth + insetAmount), 0, (imageWidth + insetAmount));
  1. 图片居上,调整 image 和 文字之间的间距
   self.imageEdgeInsets = UIEdgeInsetsMake(-titleHeight - insetAmount,(width - imageWidth)/2, 0, (width - imageWidth)/2 - titleWidth);
   self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, -imageWidth - insetAmount, 0);
  1. 图片居下,调整 image 和 文字之间的间距
   self.imageEdgeInsets = UIEdgeInsetsMake(0, (width - imageWidth)/2 , -titleHeight - insetAmount, (width - imageWidth)/2 - titleWidth);
   self.titleEdgeInsets = UIEdgeInsetsMake(-imageHeight - insetAmount, -imageWidth, 0, 0);

OC 的实现

typedef NS_ENUM(NSUInteger,ImageLocation) {
             ImageLocationLeft = 0,
             ImageLocationRight,
             ImageLocationTop,
             ImageLocationBottom};

- (void)setImage:(ImageLocation)location space:(CGFloat)space{
    CGFloat imageWidth = self.imageView.bounds.size.width;
    CGFloat imageHeight = self.imageView.bounds.size.height;
    CGFloat titleWidth = self.titleLabel.bounds.size.width;
    CGFloat titleHeight = self.titleLabel.bounds.size.height;
    CGFloat insetAmount = space / 2;
    CGFloat width = self.frame.size.width;
    if (location == ImageLocationLeft) {
        self.imageEdgeInsets = UIEdgeInsetsMake(0,-insetAmount, 0, insetAmount);
        self.titleEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, -insetAmount);
    }else if (location == ImageLocationRight){
        self.imageEdgeInsets = UIEdgeInsetsMake(0,(titleWidth + insetAmount), 0, -(titleWidth + insetAmount));
        self.titleEdgeInsets = UIEdgeInsetsMake(0,-(imageWidth + insetAmount), 0, (imageWidth + insetAmount));
    }else if (location == ImageLocationTop){
        self.imageEdgeInsets = UIEdgeInsetsMake(-titleHeight - insetAmount,(width - imageWidth)/2, 0, (width - imageWidth)/2 - titleWidth);
        self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, -imageWidth - insetAmount, 0);
    }else if (location == ImageLocationBottom){
        self.imageEdgeInsets = UIEdgeInsetsMake(0, (width - imageWidth)/2 , -titleHeight - insetAmount, (width - imageWidth)/2 - titleWidth);
        self.titleEdgeInsets = UIEdgeInsetsMake(-imageHeight - insetAmount, -imageWidth, 0, 0);
    }
}

Swift实现

public  enum  ImageLocation{
  case  left,right,top,bottom
}

/// 设置image位置(必须要先设置frame)
    /// - Parameters:
    ///   - location: 位置
    ///   - space: 标题与图片的间距
func setImage(location:ImageLocation,space:CGFloat){
        guard let label = titleLabel,let imageV = imageView else {
            return
        }
        let imageWidth = imageV.bounds.size.width
        let imageHeight = imageV.bounds.size.height
        let titleWidth = label.bounds.size.width
        let titleHeight = label.bounds.size.height
        let width = frame.size.width
        switch location {
        case .left:
            titleEdgeInsets = UIEdgeInsets.init(top: 0, left: space/2, bottom: 0, right: -space/2)
            imageEdgeInsets = UIEdgeInsets.init(top: 0, left: -space/2, bottom: 0, right: space/2)
        case .right:
            imageEdgeInsets = UIEdgeInsets.init(top: 0, left: (titleWidth + space/2), bottom: 0, right: -(titleWidth + space/2))
            titleEdgeInsets = UIEdgeInsets.init(top: 0, left:  -(imageWidth + space/2), bottom: 0, right: (imageWidth + space/2))
        case .top:
            imageEdgeInsets = UIEdgeInsets.init(top:-titleHeight - space/2, left:(width - imageWidth)/2, bottom: 0, right: (width - imageWidth)/2 - titleWidth)
            titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -imageWidth, bottom: -imageHeight - space/2, right:  0)
        case .bottom:
            imageEdgeInsets = UIEdgeInsets.init(top:0, left: (width - imageWidth)/2, bottom: -titleHeight - space/2, right: (width - imageWidth)/2 - titleWidth)
            titleEdgeInsets = UIEdgeInsets.init(top: -imageHeight - space/2, left: -imageWidth, bottom: 0, right: 0)
        }
}

如果没有提前设置frame的是无法获取到title的宽度和高度的,如果通过Snapkit或者Masonry 布局的话,可以提前设置size,或者在布局结束后再设置image的位置

classdef ImageProcessor < matlab.apps.AppBase % Properties对应GUI控件 properties (Access = public) UIFigure matlab.ui.Figure LoadButton matlab.ui.control.Button SaveButton matlab.ui.control.Button BrightnessSlider matlab.ui.control.Slider ContrastSlider matlab.ui.control.Slider GrayscaleButton matlab.ui.control.Button RotateButton matlab.ui.control.Button EdgeDetectButton matlab.ui.control.Button ImageAxes matlab.ui.control.UIAxes end properties (Access = private) OriginalImage % 存储原始图像数据 CurrentImage % 存储当前显示图像 end methods (Access = private) % 图像加载回调函数 function LoadButtonPushed(app, ~) [filename, pathname] = uigetfile({'*.jpg;*.png;*.bmp', 'Image Files'}); if filename try app.OriginalImage = imread(fullfile(pathname, filename)); app.CurrentImage = app.OriginalImage; imshow(app.CurrentImage, 'Parent', app.ImageAxes); catch errordlg('无法加载图像文件'); end end end % 图像保存回调函数 function SaveButtonPushed(app, ~) if isempty(app.CurrentImage) errordlg('没有图像可保存'); return; end [filename, pathname] = uiputfile({'*.jpg','JPEG文件'; '*.png','PNG文件'}); if filename imwrite(app.CurrentImage, fullfile(pathname, filename)); end end % 亮度调整回调函数 function BrightnessSliderValueChanged(app, ~) if isempty(app.OriginalImage) return; end adjusted = app.OriginalImage + app.BrightnessSlider.Value; app.CurrentImage = adjusted; imshow(app.CurrentImage, 'Parent', app.ImageAxes); end % 对比度调整回调函数 function ContrastSliderValueChanged(app, ~) if isempty(app.OriginalImage) return; end adjusted = imadjust(app.OriginalImage, [], [], app.ContrastSlider.Value); app.CurrentImage = adjusted; imshow(app.CurrentImage, 'Parent', app.ImageAxes); end % 灰度化回调函数 function GrayscaleButtonPushed(app, ~) if isempty(app.OriginalImage) return; end if size(app.OriginalImage,3) == 3 app.CurrentImage = rgb2gray(app.OriginalImage); imshow(app.CurrentImage, 'Parent', app.ImageAxes); end end % 旋转回调函数 function RotateButtonPushed(app, ~) if isempty(app.CurrentImage) return; end app.CurrentImage = imrotate(app.CurrentImage, 90); imshow(app.CurrentImage, 'Parent', app.ImageAxes); end % 边缘检测回调函数 function EdgeDetectButtonPushed(app, ~) if isempty(app.CurrentImage) return; end if size(app.CurrentImage,3) == 3 gray = rgb2gray(app.CurrentImage); else gray = app.CurrentImage; end edges = edge(gray, 'canny'); app.CurrentImage = edges; imshow(app.CurrentImage, 'Parent', app.ImageAxes); end end % App初始化布局 methods (Access = private) function createComponents(app) % 创建UIFigure窗口 app.UIFigure = uifigure('Name', '简易图像处理器'); app.UIFigure.Position = [100 100 800 600]; % 创建图像显示区域 app.ImageAxes = uiaxes(app.UIFigure); app.ImageAxes.Position = [50 150 500 400]; % 创建按钮控件 app.LoadButton = uibutton(app.UIFigure, 'push',... 'Position', [600 500 120 30],... 'Text', '加载图像',... 'ButtonPushedFcn', createCallbackFcn(app, @LoadButtonPushed, true)); app.SaveButton = uibutton(app.UIFigure, 'push',... 'Position', [600 450 120 30],... 'Text', '保存图像',... 'ButtonPushedFcn', createCallbackFcn(app, @SaveButtonPushed, true)); % 创建滑动条控件 app.BrightnessSlider = uislider(app.UIFigure,... 'Position', [600 400 150 3],... 'Limits', [-100 100],... 'ValueChangedFcn', createCallbackFcn(app, @BrightnessSliderValueChanged, true)); app.ContrastSlider = uislider(app.UIFigure,... 'Position', [600 350 150 3],... 'Limits', [0 2],... 'Value', 1,... 'ValueChangedFcn', createCallbackFcn(app, @ContrastSliderValueChanged, true)); % 其他功能按钮 app.GrayscaleButton = uibutton(app.UIFigure, 'push',... 'Position', [600 250 120 30],... 'Text', '灰度化',... 'ButtonPushedFcn', createCallbackFcn(app, @GrayscaleButtonPushed, true)); app.RotateButton = uibutton(app.UIFigure, 'push',... 'Position', [600 200 120 30],... 'Text', '旋转90度',... 'ButtonPushedFcn', createCallbackFcn(app, @RotateButtonPushed, true)); app.EdgeDetectButton = uibutton(app.UIFigure, 'push',... 'Position', [600 150 120 30],... 'Text', '边缘检测',... 'ButtonPushedFcn', createCallbackFcn(app, @EdgeDetectButtonPushed, true)); end end methods (Access = public) function app = ImageProcessor createComponents(app) registerApp(app, app.UIFigure); if nargout == 0 clear app end end end end添加新的功能
最新发布
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值