iOS开发:关于UILabel、UIButton、UITextField文字下划线的设置方法(涉及到富文本的知识)

真是学到老活到老啊,梳理总结了一下关于富文本的知识,涉及到UILabel、UIButton、UITextField,好多比较经典的知识点,果断写出来,存起来。

一、UILabel

在实际开发中,经常会有几个文字或者一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求。通过NSMuttableAttstring(带属性的字符串),这些需求都可以很简便的实现。


使用字符串初始化
- (id)initWithString:(NSString *)str;
eg:
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:@"皮皮虾,我们走"];

- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;
字典中存放一些属性名和属性值,

eg:

NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:15.0],NSFontAttributeName,[UIColorredColor],NSForegroundColorAttributeName,NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"皮皮虾,我们走" attributes:attributeDict];
- (id)initWithAttributedString:(NSAttributedString *)attester;
使用NSAttributedString初始化,和NSMutableString、NSString类似

使用方法:
给某一范围内文字设置多个属性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
给某一范围内文字添加某个属性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
给某一范围内文字添加多个属性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
移除某范围内的某个属性
- (void)removeAttribute:(NSString *)name range:(NSRange)range;


常见的属性及说明
NSFontAttributeName 字体
NSParagraphStyleAttributeName 段落格式
NSForegroundColorAttributeName 字体颜色
NSBackgroundColorAttributeName 背景颜色
NSStrikethroughStyleAttributeName删除线格式
NSUnderlineStyleAttributeName 下划线格式
NSStrokeColorAttributeName 删除线颜色
NSStrokeWidthAttributeName删除线宽度

1.设置UILabel文字下划线

   UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 310, 50)];
label.backgroundColor = [UIColor redColor];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setFont:[UIFont systemFontOfSize:14]];
NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"
以前以为穷不过三代的意思是穷到三代以后就不会再穷了,长大后才知道穷到第三代已经穷的连媳妇都娶不到了,也就没有第四代了"]];
NSRange contentRange = {0,[content length]};
[content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
label.attributedText = content;
[self.view addSubview:label];


2.为UILabel文字添加删除线

NSMutableAttributedString *mutableStr = [[NSMutableAttributedString alloc] initWithString:@"原价:288"];
[mutableStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, [mutableStr length])];
[mutableStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0, [mutableStr length])];
label.attributedText = mutableStr;


3.设置UILabel中指定内容的文字颜色

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"今天天气很棒"];
[AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 2)];
label.attributedText = AttributedStr;


4.设置UILabel中某个范围内字体大小

[mutableStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:16.0] range:NSMakeRange(7,2)];



二、UIButton

1.设置UIButton文字下划线

UIButton * registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
registerBtn.frame = CGRectMake(50, 120, 150, 50);
registerBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
registerBtn.titleLabel.font = [UIFont systemFontOfSize:12];
[self.clickBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"还没有账号,去注册"];
NSRange titleRange = {0,[title length]};
[title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
[registerBtn setAttributedTitle:title forState:UIControlStateNormal];
[registerBtn addTarget:self action:@selector(setregisterUsers:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:registerBtn];


2.为UIButton文字添加删除线

[title addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, [title length])];
[title addAttribute:NSStrikethroughColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0, [title length])];
[self.clickBtn setAttributedTitle:title forState:UIControlStateNormal];

3.设置UIButton某范围内文字的颜色

首先宏定义一个属性: #define Color(r,g,b,a) [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:a]

然后一句话搞定: [title addAttribute:NSForegroundColorAttributeName value:Color(62, 190, 219, 1) range:NSMakeRange(7,2)];


4.设置UIButton某个范围字体大小

[title addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:16.0] range:NSMakeRange(7,2)];


三、UITextField

1.只有UITextField下划线的输入框

方法一:直接设置

UITextField * pswTF = [[UITextField alloc] initWithFrame:CGRectMake(20,70,340,45)];
pswTF.placeholder =@"请输入密码";
UIView * onLine = [[UIView alloc]initWithFrame:CGRectMake(0,pswTF.frame.size.height-2,pswTF.frame.size.width,2)];
onLine.backgroundColor = [UIColor redColor];
[pswTF addSubview:onLine];
[self.view addSubview:pswTF];

方法二:创建父类

步骤1:新建一个继承于UITextField的类,然后在.m里面重写父类方法

.h文件:

#import
@interface UnderLiner : UITextField
@end


.m文件:

#import "UnderLiner.h"
@implementation UnderLiner
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillRect(context, CGRectMake(0, CGRectGetHeight(self.frame) - 1, CGRectGetWidth(self.frame), 1));
}
@end

步骤2:在使用它的地方,调用这个类,导入头文件,然后用UnderLiner创建使用

在需要使用的地方的.m文件里面的操作:

导入头文件:  #import "UnderLiner.h"

具体使用方法:

- (void)initUI {

UnderLiner *textField = [[UnderLiner alloc] initWithFrame:CGRectMake(10, 450, 300, 45)];
textField.placeholder = @"请输入密码";
textField.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:textField];

}


2.设置UITextField只有下划线,并且左边有图标

UITextField * pswTF = [[UITextField alloc] initWithFrame:CGRectMake(20,70,340,45)];
pswTF.font = [UIFont systemFontOfSize:15];
pswTF.placeholder = @"请输入密码";
UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0,pswTF.height-2, pswTF.width, 1)];
line.backgroundColor=[UIColor lightGrayColor];
[pswTF addSubview:line];

// 通过init来创建初始化绝大部分控件,控件都是没有尺寸
UIImageView *searchIcon = [[UIImageView alloc] init];
searchIcon.image = [UIImage imageNamed:@"pass"];
searchIcon.width = 30;
searchIcon.height = 30;
searchIcon.contentMode = UIViewContentModeCenter;
pswTF.leftView = searchIcon;
pswTF.leftViewMode = UITextFieldViewModeAlways;


不再一一的把每个知识点的效果截图出来,直接截一个综合的效果图,看一下就可以啦。。。

iOS开发:关于UILabel、UIButton、UITextField文字下划线的设置方法(涉及到富文本的知识)_第1张图片

你可能感兴趣的:(iOS开发)