http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3232|回复: 7

[新手开发之旅] iOS新手开发之旅-UI控件之UIButton按钮

[复制链接]
发表于 2018-11-29 16:21:30 | 显示全部楼层 |阅读模式
        
       文章导航  

      【iOS新手开发之旅】   http://www.sufeinet.com/thread-24000-1-1.html

        UIButton的属性和方法
      
[Objective-C] 纯文本查看 复制代码
 //创建一个Button 这里除了 alloc init 方法之外 Button类 系统提供有另一种初始化的方法[/align]    
//    typedef NS_ENUM(NSInteger, UIButtonType) {
//        UIButtonTypeCustom = 0,       -自定义风格
//        UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),
//        UIButtonTypeDetailDisclosure, -蓝色小箭头按钮,主要做详细说明
//        UIButtonTypeInfoLight,        -亮色感叹号
//        UIButtonTypeInfoDark,         -暗色感叹号
//        UIButtonTypeContactAdd,       -十字加号按钮
//        UIButtonTypeRoundedRect = UIButtonTypeSystem,  -圆角矩形
//    };
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; //上面为系统提供的一些风格
    //设置button frmae
    button.frame = CGRectMake(100, 100, 180, 100);
    
    //设置button 背景颜色
//    button.backgroundColor = [UIColor orangeColor];
    //添加button的标题
    [button setTitle:"这是一个按钮" forState:UIControlStateNormal];
    //设置标题的颜色
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //设置标题字体的大小
    button.titleLabel.font = [UIFont systemFontOfSize:20];
    //设置button的背景图片
    [button setBackgroundImage:[UIImage imageNamed:"bear.jpeg"] forState:UIControlStateNormal];
    //获取指定状态下的背景图片
//    UIImage *tempImage = [button imageForState:UIControlStateNormal];
    //设置前景图片  前景图片必须是镂空图,或者是线条勾勒的图片
//    [button setImage:[UIImage imageNamed"pre.jpeg"] forState:UIControlStateNormal];
    //设置阴影颜色
    [button setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];
    
    /* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
//    以下是几种状态
//    enum {
//        UIControlStateNormal = 0, 常规状态显现
//        UIControlStateHighlighted = 1 << 0, 高亮状态显现
//        UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
//        UIControlStateSelected = 1 << 2, 选中状态
//        UIControlStateApplication = 0x00FF0000, 当应用程序标志时
//        UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
//    };

    //给按钮添加点击事件
    [button addTarget:self action:selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
    
//    枚举值:
//    UIControlEventTouchDown             // 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
//    UIControlEventTouchDownRepeat       // 多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
//    UIControlEventTouchDragInside       // 当一次触摸在控件窗口内拖动时。
//    UIControlEventTouchDragOutside      // 当一次触摸在控件窗口之外拖动时。
//    UIControlEventTouchDragEnter        // 当一次触摸从控件窗口之外拖动到内部时
//    UIControlEventTouchDragExit         // 当一次触摸从控件窗口内部拖动到外部时。
//    UIControlEventTouchUpInside         // 所有在控件之内触摸抬起事件
//    UIControlEventTouchUpOutside        // 所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
//    UIControlEventTouchCancel           // 所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
//
//    UIControlEventValueChanged          // 当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
//
//    UIControlEventEditingDidBegin       // 当文本控件中开始编辑时发送通知
//    UIControlEventEditingChanged        // 当文本控件中的文本被改变时发送通知。
//    UIControlEventEditingDidEnd         // 当文本控件中编辑结束时发送通知。
//    UIControlEventEditingDidEndOnExit   // 当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
//
//    UIControlEventAllTouchEvents        // 通知所有触摸事件。
//    UIControlEventAllEditingEvents      // 通知所有关于文本编辑的事件。
//    UIControlEventApplicationReserved   // range available for application use
//    UIControlEventSystemReserved        // range reserved for internal framework use
//
//    UIControlEventAllEvents             // 通知所有事件
    
    [self.view addSubview:button];
        //这里用来显示点击的按钮标题 这里声明为全局变量
    lbl = [[UILabel alloc]initWithFrame:CGRectMake(100, button.frame.origin.y + button.frame.size.height + 20, 180, 60)];
    lbl.backgroundColor = [UIColor yellowColor];
    lbl.numberOfLines = 0;
    [self.view addSubview:lbl];


       点击事件
      
[Objective-C] 纯文本查看 复制代码
-(void)clickAction:(UIButton *)sender{
    
    lbl.text = [NSString stringWithFormat:"点击的按钮标题:%@",sender.titleLabel.text];
}

       运行效果:

       Untitled.gif

     说明:
     
      [button addTarget: self action: @selector (clickAction: )  forControlEvents:UIControlEventTouchUpInside];
      这个是添加点击事件的方法:self 表示执行此事件的对象类为当前页面 @selector (clickAction: )  表示添加一个方法名字为:clickAction  这里添加冒号表示事件可传递对象也是在事件中可获取点击的对象,不添加冒号就表示单纯只是一个方法,不传递任何对象。注:如果
@selector ( clickAction: )这里加了冒号,下面的事件实现方法不是这样写的-(void)clickAction: (UIButton *)sender 也就是没有冒号以及后面的那部分,运行程序会崩溃。因为这样程序会提示找不到此方法。


       附件: Test-button.zip (76.74 KB, 下载次数: 3)

本帖被以下淘专辑推荐:



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2018-11-29 16:29:06 | 显示全部楼层
   

      
[C#] 纯文本查看 复制代码
//button 一些其他属性
@property(nonatomic)  UIEdgeInsets contentEdgeInsets; 内容内距离
@property(nonatomic)  UIEdgeInsets titleEdgeInsets;   标题内距离
@property(nonatomic)  BOOL  reversesTitleShadowWhenHighlighted; 标题的阴影改变时,按钮是否高亮显示。默认为NO
@property(nonatomic)  UIEdgeInsets imageEdgeInsets;   图片内边距
@property(nonatomic)  BOOL adjustsImageWhenHighlighted;按钮高亮的情况下,图像的颜色是否要加深一点。默认是YES
@property(nonatomic)  BOOL adjustsImageWhenDisabled; 按钮禁用的情况下,图像的颜色是否要加深一点。默认是YES
@property(nonatomic)  BOOL showsTouchWhenHighlighted; 按下按钮是否会发光 默认是NO
@property(nonatomic,readonly) UIButtonType buttonType; button的类型
 楼主| 发表于 2018-11-29 16:29:56 | 显示全部楼层
获取button 的属性方法

获取按钮当前标题
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;

获取按钮当前标题颜色
@property(nonatomic,readonly,strong) UIColor  *currentTitleColor;

获取按钮当前阴影标题颜色
@property(nullable, nonatomic,readonly,strong) UIColor *currentTitleShadowColor;

获取按钮当前按钮内图像
@property(nullable, nonatomic,readonly,strong) UIImage  *currentImage;

获取按钮当前标题背景图片
@property(nullable, nonatomic,readonly,strong) UIImage  *currentBackgroundImage;

获取按钮当前标题富文本
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle

@property(nullable, nonatomic,readonly,strong) UILabel    *titleLabel NS_AVAILABLE_IOS(3_0);
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView  NS_AVAILABLE_IOS(3_0);

指定背景边界
- (CGRect)backgroundRectForBounds: (CGRect)bounds;
指定内容边界
- (CGRect)contentRectForBounds: (CGRect)bounds;
指定标题边界
- (CGRect)titleRectForContentRect: (CGRect)contentRect;
指定图片边界
- (CGRect)imageRectForContentRect: (CGRect)contentRect;

示例:
- (CGRect)imageRectForContentRect: (CGRect)bounds{
        return CGRectMake(0.0, 0.0, 44, 44);
    }
发表于 2018-11-29 17:27:13 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-11-29 17:47:23 | 显示全部楼层
楼主更新的很快啊,楼主加油,我们都看好你哦。
发表于 2018-11-29 18:02:30 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-11-29 19:37:26 | 显示全部楼层
看到这帖子真是高兴!
发表于 2018-12-1 10:25:18 | 显示全部楼层
看到这帖子真是高兴!
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-6-8 11:12

© 2014-2021

快速回复 返回顶部 返回列表