zoukankan      html  css  js  c++  java
  • iOS 自定义UIButton(图片和文字混合)

     // UIApplicationDelegate  .h文件

    #import <UIKit/UIKit.h>

    @interface AppDelegate : UIResponder <UIApplicationDelegate>

    @property (strong, nonatomic) UIWindow *window;

    @end

     // UIApplicationDelegate  .m文件

    #import "AppDelegate.h"

    #import "CustomButton.h"

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        // Override point for customization after application launch.

        self.window.backgroundColor = [UIColor whiteColor];

      

        CustomButton *customBtn = [[CustomButton alloc] init];

        customBtn.frame = CGRectMake(100,150, 150, 60);

        [customBtn setTitle:@"十二年" forState:0];

        [customBtn setImage:[UIImage imageNamed:@"De.png"] forState:0];

        [self.window addSubview:customBtn];

        [self.window makeKeyAndVisible];

        return YES;

    }

    @end

    // 自定义CustomButton类,继承UIButton

    // CustomButton .h文件

    #import <UIKit/UIKit.h>

    @interface CustomButton : UIButton

    @end

    // CustomButton  .m文件

    #import "CustomButton.h"

    @implementation CustomButton

    -(id)initWithFrame:(CGRect)frame

    {

        self = [super initWithFrame:frame];

        if (self) {

            //设置粗体文字,以及文字的大小

            self.titleLabel.font = [UIFont boldSystemFontOfSize:20];

            // 设置文字居右

            self.titleLabel.textAlignment = NSTextAlignmentRight;

            // 设置文字的颜色

            [self setTitleColor:[UIColor redColor] forState:0];

            // 设置背景图片

            self.backgroundColor = [UIColor greenColor];

        }

        return self;

    }

    - (CGRect)titleRectForContentRect:(CGRect)contentRect

    {

        CGRect titleFrame = CGRectMake(0, 0, contentRect.size.width * 0.6, contentRect.size.height);

        return titleFrame;

    }

    - (CGRect)imageRectForContentRect:(CGRect)contentRect

    {

        return CGRectMake(contentRect.size.width * 0.6, 0, contentRect.size.width * 0.4, contentRect.size.height);

    }

    @end

  • 相关阅读:
    dig命令不能使用(-bash: dig: command not found)
    linux系统中的一些典型问题汇总
    Django运行项目时候出现DisallowedHost at / Invalid HTTP_HOST header:
    Grafana添加Zabbix为数据源(二)
    Grafana添加Zabbix为数据源(一)
    linux go环境安装
    centos6里面装zabbix(五)
    centos6里面装zabbix(二)
    HTTP状态码分类及异常状态码处理
    超详细 Linux 下编译安装Redis 以及php配套使用
  • 原文地址:https://www.cnblogs.com/lantu1989/p/4594156.html
Copyright © 2011-2022 走看看