你真的了解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. ...
随机推荐
- html/css/js小技巧实例
一些学习中碰到的小技巧 让div自动撑起来: .clearfix:after{ content: "."; clear: both; display: block; visibil ...
- 纯Shading Language绘制飞机火焰效果
上篇<纯Shading Language绘制HTML5时钟>体现了GLSL可编程性特点,但没有体现GLSL可编程出各种酷炫效果的特点,今天我们将用纯Shading Language绘制火焰 ...
- react路由案例(非常适合入门)
前面已经已经讲过一次路由 稍微有些复杂 考虑到有些同学刚接触到 我准备了一个简单的demo 就当自己也顺便复习一下 data.js const data = [ { name: 'Ta ...
- 相关子查询【SQL Server】
查询book表中大于该类图书价格平均值的图书信息 先将第一条记录的类编号的值为2代入子查询中,子查询为 select avg(price) from book b where b.id=2 则得到类编 ...
- Razor语法中链接的一些方法
在Razor中,有HTML.ActionLink和 Url.Action来呈现链接.它们有什么区别呢.能分清了,就知道在什么情况之下使用它们了. 首先来看html.ActionLink,这个方法重载挺 ...
- C# 委托和事件(一):最简单的委托和事件
C#的事件基于委托,所以先说委托. 一切脱离实际场景的抽象概念新手看上去就像是在扯犊子,不错,我就是个新手.所以我需要一个实际的场景. 明天刚好考试(商务英语),考试上有两个角色(class):老师( ...
- Java总结篇系列:类型转换/造型
Java中,经常可以遇到类型转换的场景,从变量的定义到复制.数值变量的计算到方法的参数传递.基类与派生类间的造型等,随处可见类型转换的身影.Java中的类型转换在Java编码中具有重要的作用.首先,来 ...
- Scalaz(7)- typeclass:Applicative-idomatic function application
Applicative,正如它的名称所示,就是FP模式的函数施用(function application).我们在前面的讨论中不断提到FP模式的操作一般都在管道里进行的,因为FP的变量表达形式是这样 ...
- Struct2
1.MVC思想概述 Servlet作为前端控制器,负责接收客户端发送的请求,在Servlet中只包含控制逻辑和简单的前端处理:然后调用后端javabean来完成实际的逻辑处理: 最后转发到相应的jsp ...
- jvm三的三种类加载器
JVM有三种类加载器:bootstrap负责加载系统类,extclassloader负责加载扩展类,appclassloader负责加载应用类.他们主要是分工不一样,各自负责不同的区域,另外也是为了实 ...