自定义排序按钮二

本文介绍了如何在UIButton上实现图文混排的功能,通过使用NSAttributedString和NSAttributedStringDrawingOptions,结合使用NSFont、NSForegroundColor等属性,以及调整图像和文本的位置,实现了按钮的高大上效果。

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

之前做了个排序按钮是继承UIButton来实现的, 这次在UIbutton上添加 (高大上就是所谓的图文混排),主要还是EdgeInsets。

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CustomDropBtn : NSObject
+(UIButton *)DropBtnWithFrame:(CGRect) farme withTitle:(NSString *)title;
@end
#import "CustomDropBtn.h"
#define RGB(r,g,b)                  [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]

@implementation CustomDropBtn
+(UIButton *)DropBtnWithFrame:(CGRect) frame withTitle:(NSString *)title
{
    NSDictionary *attributes=@{NSFontAttributeName: [UIFont systemFontOfSize:20.f],NSForegroundColorAttributeName:RGB(26, 26, 26)};
    CGRect titleframe=[title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    
    UIButton *dropBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [dropBtn setFrame:frame];
    [dropBtn setImage:[UIImage imageNamed:@"yzp_data_dropdown.png"] forState:UIControlStateNormal];
    [dropBtn setImage:[UIImage imageNamed:@"yzp_data_dropback.png"] forState:UIControlStateSelected];
    [dropBtn setImageEdgeInsets:UIEdgeInsetsMake((frame.size.height-5)/2, frame.size.width-20-8, (frame.size.height-5)/2, 20)];
    [dropBtn setTitle:title forState:UIControlStateNormal];
    [dropBtn setTitleColor:RGB(26, 26, 26) forState:UIControlStateNormal];
    [dropBtn.titleLabel setFont:[UIFont systemFontOfSize:20.0]];
    
    [dropBtn setTitleEdgeInsets:UIEdgeInsetsMake((frame.size.height-titleframe.size.height)/2,(frame.size.width-titleframe.size.width)/2-28, (frame.size.height-titleframe.size.height)/2,0)];
    
    [dropBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    return dropBtn;
}

@end

 

转载于:https://www.cnblogs.com/5ishare/p/4885232.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值