一:首先查看一下关于UIButton的定义

@class UIImage, UIFont, UIColor, UIImageView, UILabel;

//设置UIButton的样式
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = , // 自定义,无风格
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button UIButtonTypeDetailDisclosure, //蓝色的披露按钮,可放在任何文字旁
UIButtonTypeInfoLight, //微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁
UIButtonTypeInfoDark, //白色背景下使用的深色圆圈信息按钮
UIButtonTypeContactAdd, //蓝色加号(+)按钮,可以放在任何文字旁 UIButtonTypeRoundedRect = UIButtonTypeSystem, // 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片
}; NS_CLASS_AVAILABLE_IOS(2_0) @interface UIButton : UIControl <NSCoding> + (instancetype)buttonWithType:(UIButtonType)buttonType; @property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // 默认值 UIEdgeInsetsZero 所有按钮的内容周围的矩形的内侧或者外侧的边缘
@property(nonatomic) UIEdgeInsets titleEdgeInsets; // 默认值UIEdgeInsetsZero 设置文字的EdgeInsets 在按钮标题文本周围矩形的,向内或者向外的边缘
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; // 默认值NO.当按钮高亮时,是否更改标题阴影
@property(nonatomic) UIEdgeInsets imageEdgeInsets; // 默认值 UIEdgeInsetsZero 设置UIButton上的图标间距 属于按钮图片周围矩形的,向内或者向外的边缘
@property(nonatomic) BOOL adjustsImageWhenHighlighted; // 默认值为YES.默认情况下,在按钮被禁用时,图像会被画的颜色深一些。要禁用此功能,请将这个属性设置为NO:
@property(nonatomic) BOOL adjustsImageWhenDisabled; // 默认值为YES.默认情况下,按钮在被禁用时,图像会被画的颜色淡一些。要禁用此功能,请将这个属性设置为NO:
@property(nonatomic) BOOL showsTouchWhenHighlighted; // 默认值为 NO. 这个属性设置为YES,可令按钮在按下时发光
@property(null_resettable, nonatomic,strong) UIColor *tintColor NS_AVAILABLE_IOS(5_0); // The tintColor is inherited through the superview hierarchy. See UIView for more information. //获取当前UIButton的UIButtonType 只读
@property(nonatomic,readonly) UIButtonType buttonType; //常见的设置属性效果
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR;
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR;
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR;
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line - (nullable NSString *)titleForState:(UIControlState)state;
- (nullable UIColor *)titleColorForState:(UIControlState)state;
- (nullable UIColor *)titleShadowColorForState:(UIControlState)state;
- (nullable UIImage *)imageForState:(UIControlState)state;
- (nullable UIImage *)backgroundImageForState:(UIControlState)state;
- (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0); //获取当前UIButton的一些属性值 都为只读
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle; // normal/highlighted/selected/disabled. can return nil
@property(nonatomic,readonly,strong) UIColor *currentTitleColor; // normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)
@property(nullable, nonatomic,readonly,strong) UIColor *currentTitleShadowColor; // normal/highlighted/selected/disabled.
@property(nullable, nonatomic,readonly,strong) UIImage *currentImage; // normal/highlighted/selected/disabled. can return nil
@property(nullable, nonatomic,readonly,strong) UIImage *currentBackgroundImage; // normal/highlighted/selected/disabled. can return nil
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); // normal/highlighted/selected/disabled. can return nil @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;
@end

UIButton是继承于UIControl,并且也遵循NSCoding的协议;

知识点1:关于UIControlState的Enum值

enum{
UIControlStateNormal       = ,//常态
UIControlStateHighlighted  =  << ,//高亮
UIControlStateDisabled     =  << ,//禁用
UIControlStateSelected     =  << ,//选中
UIControlStateApplication  = 0x00FF0000,//当应用程序标志使用时
UIControlStateReserved     = 0xFF000000//为内部框架预留的
};
typedef NSUInteger UIControlState;

知识点2:contentHorizontalAlignment;可以设置UIButton文字的对齐方式,contentHorizontalAlignment并不是UIButton的属性,而是它父类UIControl的一个属性;

typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
UIControlContentHorizontalAlignmentCenter = ,
UIControlContentHorizontalAlignmentLeft = ,
UIControlContentHorizontalAlignmentRight = ,
UIControlContentHorizontalAlignmentFill = ,
};

知识点3:forControlEvents参数类型

typedef NS_OPTIONS(NSUInteger, UIControlEvents)
{
UIControlEventTouchDown                 =  <<  ,//单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat      =  <<  ,//多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside         =  <<  ,//当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside       =  <<  ,//当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter           =  <<  ,//当一次触摸从控件窗口之外拖动到内部时
UIControlEventTouchDragExit             =  <<  ,//当一次触摸从控件窗口内部拖动到外部时。
UIControlEventTouchUpInside            =  <<  ,//所有在控件之内触摸抬起事件
UIControlEventTouchUpOutside          =  <<  ,//所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel                =  <<  ,//所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventValueChanged             =  << ,//当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin           =  << ,//当文本控件中开始编辑时发送通知
UIControlEventEditingChanged           =  << ,//当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd              =  << ,//当文本控件中编辑结束时发送通知。
UIControlEventEditingDidEndOnExit    =  << ,//当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAllTouchEvents             = 0x00000FFF,//通知所有触摸事件。
UIControlEventAllEditingEvents           = 0x000F0000,//通知所有关于文本编辑的事件。
UIControlEventApplicationReserved    = 0x0F000000,//range available for application use
UIControlEventSystemReserved          = 0xF0000000,//range reserved for internal framework use
UIControlEventAllEvents                      = 0xFFFFFFFF//通知所有事件
};

二:首先查看一下关于UILabel 的定义

@class UIColor, UIFont;

NS_CLASS_AVAILABLE_IOS(2_0) @interface UILabel : UIView <NSCoding>

@property(nullable, nonatomic,copy)   NSString           *text;            // 设置文本内容
@property(null_resettable, nonatomic,strong) UIFont *font; // 设置文本字体,默认是系统17
@property(null_resettable, nonatomic,strong) UIColor *textColor; // 设置文本色
@property(nullable, nonatomic,strong) UIColor *shadowColor; // 设置文本的阴影
@property(nonatomic) CGSize shadowOffset; // 默认值CGSizeMake(0, -1)
@property(nonatomic) NSTextAlignment textAlignment; // 默认值 NSTextAlignmentLeft 文字位置排版
@property(nonatomic) NSLineBreakMode lineBreakMode; // default is NSLineBreakByTruncatingTail. used for single and multiple lines of text // 富文本属性
@property(nullable, nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); // 默认值 nil @property(nullable, nonatomic,strong) UIColor *highlightedTextColor; // 默认值 nil
@property(nonatomic,getter=isHighlighted) BOOL highlighted; // 默认值 NO @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // 默认值为 NO 所以它没有点击事件效果
@property(nonatomic,getter=isEnabled) BOOL enabled; // 默认值为YES. //行数 0表示不限制
@property(nonatomic) NSInteger numberOfLines; @property(nonatomic) BOOL adjustsFontSizeToFitWidth; // 默认值为 NO 文字的大小是否根据宽度来自动调整
@property(nonatomic) UIBaselineAdjustment baselineAdjustment; // default is UIBaselineAdjustmentAlignBaselines
@property(nonatomic) CGFloat minimumScaleFactor NS_AVAILABLE_IOS(6_0); // default is 0.0 @property(nonatomic) BOOL allowsDefaultTighteningForTruncation NS_AVAILABLE_IOS(9_0); // default is NO //重写UILabel布局
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect; //这个属性是用来设置多行label的最大宽度的
//当自动布局的时候约束这个label的时候这个属性会起作用
//在自动布局添加约束中,若文本超过了指定的最大宽度的时候 文本会另起一行 从而增加了label的高度
@property(nonatomic) CGFloat preferredMaxLayoutWidth NS_AVAILABLE_IOS(6_0); // IOS7以后就弃用
@property(nonatomic) CGFloat minimumFontSize NS_DEPRECATED_IOS(2_0, 6_0); // deprecated - use minimumScaleFactor. default is 0.0 // IOS7以后就弃用
@property(nonatomic) BOOL adjustsLetterSpacingToFitWidth NS_DEPRECATED_IOS(6_0,7_0); @end

UILabel是继承于UIView,并且也遵循NSCoding的协议;

知识点1:枚举类型 默认是NSLineBreakByTruncatingTail

NSLineBreakByWordWrapping = , 按着一个单词来显示 不会被剪辑剩余的不会被显示
NSLineBreakByCharWrapping,   按着一个字体来显示 不会被剪辑剩余的不会被显示
NSLineBreakByClipping,         把能显示的全显示完 剩下的直接不显示可能有的字显示一半就被剪辑
NSLineBreakByTruncatingHead,   在那一行显示不全的话 那一行 就以 ...abcd模式来显示
NSLineBreakByTruncatingTail,   在那一行显示不全的话 那一行 就以 abcd...模式来显示
NSLineBreakByTruncatingMiddle 在那一行显示不全的话那一行 就以 ab...cd模式来显示

知识点2:textAlignment是设置label的对齐方式是一个枚举,默认是左对齐

NSTextAlignmentLeft=     左对齐
NSTextAlignmentCenter=    居中
NSTextAlignmentRight=     右对齐
NSTextAlignmentJustified= 左右两边都对齐 一个段落的最后一行是natural-aligned
NSTextAlignmentNatural=   显示脚本的默认对齐方式

知识点3:全部NSMutableAttributedString属性

    NSFontAttributeName;                  //字体,value是UIFont对象
NSParagraphStyleAttributeName; //绘图的风格(居中,换行模式,间距等诸多风格),value是NSParagraphStyle对象
NSForegroundColorAttributeName; // 文字颜色,value是UIFont对象
NSBackgroundColorAttributeName; // 背景色,value是UIFont
NSLigatureAttributeName; // 字符连体,value是NSNumber
NSKernAttributeName; // 字符间隔
NSStrikethroughStyleAttributeName; //删除线,value是NSNumber
NSUnderlineStyleAttributeName; //下划线,value是NSNumber
NSStrokeColorAttributeName; //描绘边颜色,value是UIColor
NSStrokeWidthAttributeName; //描边宽度,value是NSNumber
NSShadowAttributeName; //阴影,value是NSShadow对象
NSTextEffectAttributeName; //文字效果,value是NSString
NSAttachmentAttributeName; //附属,value是NSTextAttachment 对象
NSLinkAttributeName; //链接,value是NSURL or NSString
NSBaselineOffsetAttributeName; //基础偏移量,value是NSNumber对象
NSUnderlineColorAttributeName; //下划线颜色,value是UIColor对象
NSStrikethroughColorAttributeName; //删除线颜色,value是UIColor
NSObliquenessAttributeName; //字体倾斜
NSExpansionAttributeName; //字体扁平化
NSVerticalGlyphFormAttributeName; //垂直或者水平,value是 NSNumber,0表示水平,1垂直

实例运用:

/************************************************某区域内************************************************************/
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:@"0元"];
[string setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:]} range:NSMakeRange(string.length-, )];
etlbl.attributedText = string;
/************************************************基本用法************************************************************/
NSString *content = @"内容太多,需要自适应才能解决问题,所以需要写这个扩展类,内容太多,需要自适应才能解决问题,所以需要写这个扩展类"; NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:content];
//字体大小
[string addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:]
range:NSMakeRange(, )];
//字体颜色
[string addAttribute:NSForegroundColorAttributeName
value:[UIColor yellowColor]
range:NSMakeRange(, )];
//字体背景颜色
[string addAttribute:NSBackgroundColorAttributeName
value:[UIColor purpleColor]
range:NSMakeRange(, )]; //添加下划线
[string addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:NSMakeRange(, )];
//添加下划线颜色
[string addAttribute:NSUnderlineColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(, )]; UILabel *etlbl3 = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
etlbl3.attributedText = string;
[self.view addSubview:etlbl3];

知识点4:iOS的UILabel设置居上对齐,居中对齐,居下对齐

在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐。

#import <UIKit/UIKit.h>
typedef enum
{
VerticalAlignmentTop = , // default
VerticalAlignmentMiddle,
VerticalAlignmentBottom,
} VerticalAlignment;
@interface myUILabel : UILabel
{
@private
VerticalAlignment _verticalAlignment;
} @property (nonatomic) VerticalAlignment verticalAlignment; @end
#import "myUILabel.h"  

@implementation myUILabel
@synthesize verticalAlignment = verticalAlignment_; - (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.verticalAlignment = VerticalAlignmentMiddle;
}
return self;
} - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
verticalAlignment_ = verticalAlignment;
[self setNeedsDisplay];
} - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
switch (self.verticalAlignment) {
case VerticalAlignmentTop:
textRect.origin.y = bounds.origin.y;
break;
case VerticalAlignmentBottom:
textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
break;
case VerticalAlignmentMiddle:
// Fall through.
default:
textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
}
return textRect;
} -(void)drawTextInRect:(CGRect)requestedRect {
CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
[super drawTextInRect:actualRect];
} @end

运用(实现左上的效果):

lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(, , , )];
UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];//使用半透明图片作为label的背景色
lbl_mylabel.backgroundColor = color;
lbl_mylabel.textAlignment = UITextAlignmentLeft;
lbl_mylabel.textColor = UIColor.whiteColor;
lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap;
lbl_mylabel.numberOfLines = ;
[lbl_mylabel setVerticalAlignment:VerticalAlignmentTop];
[self addSubview:lbl_mylabel];

上面的实例就是针对下面两个方法的运用:

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect;

最近有个妹子弄的一个关于扩大眼界跟内含的订阅号,每天都会更新一些深度内容,在这里如果你感兴趣也可以关注一下(嘿对美女跟知识感兴趣),当然可以关注后输入:github 会有我的微信号,如果有问题你也可以在那找到我;当然不感兴趣无视此信息;

你真的了解UIButton、UILabel 吗?的更多相关文章

  1. swift系统学习控件篇:UIbutton+UIlabel+UITextField+UISwitch+UISlider

    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UIButton+UILabel // // ViewController.swift // ...

  2. 最近读cocoaui源代码有感

    上半年为了做一个ios的应用,引入了cocoaui库,主要是用来布局ios界面,发现简化了不少代码和工作量.因为在写第一个ios应用的时候,用的代码布局,在适配4s和6的机型时候,几乎被搞死,大量的约 ...

  3. MVC模式在UI里的应用

    In a similar way to other parts of a game, user interfaces usually go through several iterations unt ...

  4. UI--普通控件总结1--控件使用

    本文目录 0.UIView常用的属性和操作 0_1.UIView常见的属性 0_2.UIView状态 0_3.UIView常用的方法 1.文本框UITextField和文本视图UITextView 1 ...

  5. UI入门指引

    1. iOS学习路线: C语言:数据类型.流程控制.函数.指针.字符串.结构体.枚举.预处理: OC:面向对象.内存管理.分类.协议.Block.KVC/KVO.Foundation框架: iOS基础 ...

  6. 七、考反映小游戏《苹果iOS实例编程入门教程》

    该app为应用的功能为一个简单的考反应游戏 纲要:-UIButton, UILabel, UIImageView 的运用:-利用rendom增加游戏可玩性: 游戏说明: 在按下开始游戏后,分为三盏的指 ...

  7. iOS 设置页面的代码编写

    突然觉得好久没有更新博客了,今天就想把自己的项目中的一些功能和常用的模块写出来给大家参考一下... 这是我的二个项目中的不同的设置界面,第一个设置的那个按钮是 用的开关switch ,当然这个就容易一 ...

  8. iOS开发-Alpha,Hidden与Opaque区别

    UIView中的这三个属性用的比较多,尤其是Alpha和Opaque之间有的时候不是很好分别,稍微整理下: Alpha(不透明度) alpha是不透明度,属性为浮点类型的值,取值范围从0到1.0,表示 ...

  9. iOS完整学习步骤

    一  C语言 1.1基本数据类型和基本运算 1.2 函数 数组 字符串 指针 1.3 预处理指令 1.4结构体 枚举 1.5 文件操作 内存管理 二 Objective - C 2.1 面向对象 2. ...

随机推荐

  1. 通过自定义相册来介绍photo library的使用

    因为我在模仿美图秀秀的功能,在使用相册时候,UIImagePickerController本来就是一个UINavigationController的子类,所以没有办法使用push,所以做了一个自定义的 ...

  2. 四、BLE(中)

    1.1       GATT Manager GATT MGR模块管理所有的GATT服务,同时也是连接GATT模块与GATT ServiceS模块的桥梁. 1.1.1    主要功能模块 先来看一张该 ...

  3. noip模拟赛 好元素 哈希表的第一题

    这是一道关于 题2好元素 2s [问题描述] 小A一直认为,如果在一个由N个整数组成的数列{An}中,存在以下情况: Am+An+Ap = Ai (1 <= m, n, p < i < ...

  4. MyEclipse中常用的快捷键大全,快来.....

    在这里分享点常用的快捷键,希望对你有帮助! Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] ...

  5. Emit学习(3) - OpCodes - 循环和异常

    本来准备直接进入Dapper的, 但是昨天初步看了一下, 内容不少, Dapper不愧是一款成熟的框架, 里面对各种情况的考虑, 很实用, 不过这也使得我短时间内看不完, 所以得等几天了. 那就先来看 ...

  6. .Net语言 APP开发平台——Smobiler学习日志:Poplist控件在APP中的应用场景以及代码

    最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的”Smobil ...

  7. 有关微信开发中errorcode:-1 errmsg:system error 错误的一点原因

    如果你在微信开发中遇到{"errcode":-1,"errmsg":"system error,hints:[req_id:]"}这样的错误 ...

  8. 孙鑫MFC学习笔记19:动态链接库

    1.动态链接库 2.静态库和动态库 3.使用动态链接库的好处 4.动态链接库编译需要导出,使用需要导入 __declspec(dllexport),__declspec(dllimport) 5.动态 ...

  9. GitHub上下载源代码的方法

    GitHub上找到自己要下载的项目以后,有3种方法可以下载源代码. 第一种是复制该项目的地址,然后用其他软件下载: 第二种是安装了官方客户端,可以直接点击"Clone in Desktop& ...

  10. [iOS] Delphi for iOS 版本需求

    Delphi iOS 版本需求 版本 版本需求 官网 备注 Delphi 10.1 Berlin iPod Touch, iPhone, or iPad iOS 8 iOS 9 http://docw ...