你真的了解UIButton、UILabel 吗?
一:首先查看一下关于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 吗?的更多相关文章
- swift系统学习控件篇:UIbutton+UIlabel+UITextField+UISwitch+UISlider
工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UIButton+UILabel // // ViewController.swift // ...
- 最近读cocoaui源代码有感
上半年为了做一个ios的应用,引入了cocoaui库,主要是用来布局ios界面,发现简化了不少代码和工作量.因为在写第一个ios应用的时候,用的代码布局,在适配4s和6的机型时候,几乎被搞死,大量的约 ...
- MVC模式在UI里的应用
In a similar way to other parts of a game, user interfaces usually go through several iterations unt ...
- UI--普通控件总结1--控件使用
本文目录 0.UIView常用的属性和操作 0_1.UIView常见的属性 0_2.UIView状态 0_3.UIView常用的方法 1.文本框UITextField和文本视图UITextView 1 ...
- UI入门指引
1. iOS学习路线: C语言:数据类型.流程控制.函数.指针.字符串.结构体.枚举.预处理: OC:面向对象.内存管理.分类.协议.Block.KVC/KVO.Foundation框架: iOS基础 ...
- 七、考反映小游戏《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的考反应游戏 纲要:-UIButton, UILabel, UIImageView 的运用:-利用rendom增加游戏可玩性: 游戏说明: 在按下开始游戏后,分为三盏的指 ...
- iOS 设置页面的代码编写
突然觉得好久没有更新博客了,今天就想把自己的项目中的一些功能和常用的模块写出来给大家参考一下... 这是我的二个项目中的不同的设置界面,第一个设置的那个按钮是 用的开关switch ,当然这个就容易一 ...
- iOS开发-Alpha,Hidden与Opaque区别
UIView中的这三个属性用的比较多,尤其是Alpha和Opaque之间有的时候不是很好分别,稍微整理下: Alpha(不透明度) alpha是不透明度,属性为浮点类型的值,取值范围从0到1.0,表示 ...
- iOS完整学习步骤
一 C语言 1.1基本数据类型和基本运算 1.2 函数 数组 字符串 指针 1.3 预处理指令 1.4结构体 枚举 1.5 文件操作 内存管理 二 Objective - C 2.1 面向对象 2. ...
随机推荐
- 第3/24周 区_SQL Server中管理空间的基本单位
哇哦,SQL Server性能调优培训已经进入第3周了!同时你已经对SQL Server内核运行机制有了很好的认识.今天我会讲下SQL Server中的区管理,因为这是个很重要的话题,我们会在第23周 ...
- SQL Server 2014如何提升非在线的在线操作
在今天的文章里,我想谈下在线索引重建操作( Online Index Rebuild operations),它们在SQL Server 2014里有怎样的提升.我们都知道,自SQL Server 2 ...
- 转[开发环境配置]在Ubuntu下配置舒服的Python开发环境
在Ubuntu下配置舒服的Python开发环境 Ubuntu 提供了一个良好的 Python 开发环境,但如果想使我们的开发效率最大化,还需要进行很多定制化的安装和配置.下面的是我们团队开发人员推荐的 ...
- 微信开发中遇到“当前页面的url未注册”问题
在公众号后台设置支付授权目录,完全按照规则去设定的,但是在实际测试中会出现“当前页面的url未注册”的问题,反复确认后发现目录并没有问题,唯一的区别就是大小写不同. 但是一般url不都是不区分大小写的 ...
- C#自定义特性实例
元数据,就是C#中封装的一些类,无法修改.类成员的特性被称为元数据中的注释. 1.什么是特性 (1)属性与特性的区别 属性(Property):属性是面向对象思想里所说的封装在类里面的数据字段, ...
- iOS阶段学习第14天笔记(NSString与NSMutableString)
iOS学习(OC语言)知识点整理 一.OC字符串的操作 1)OC中字符串分为两种: 1.不可变字符串NSString:不能修改对象内容,但是可以改变对象的指针. 2.可变字符串NSMutableStr ...
- iOS 阶段学习第九天笔记(内存管理)
iOS学习(C语言)知识点整理 一.内存管理 1)malloc , 用于申请内存; 结构void *malloc(size_t),需要引用头文件<stdlib.h>:在堆里面申请内存,si ...
- .NET 中获取调用方法名
在写记录日志功能时,需要记录日志调用方所在的模块名.命名空间名.类名以及方法名,想到使用的是反射(涉及到反射请注意性能),但具体是哪一块儿还不了解,于是搜索,整理如下: 需要添加相应的命名空间: us ...
- Spring常用注解汇总
本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component- ...
- 转:python signal信号
转自:http://www.jb51.net/article/74844.htm 在liunx系统中要想每隔一分钟执行一个命令,最普遍的方法就是crontab了,如果不想使用crontab,经同事指点 ...