iOS中给ios button下划线设置下划线,怎么将下划线和标题的距离分开一点

ios 关于UILabel、UIButton、UITextField文字下划线的设置方法(涉及到富文本的知识)_IOS开发-织梦者
当前位置:&>&&>& > ios 关于UILabel、UIButton、UITextField文字下划线的设置方法(涉及到富文本的知识)
ios 关于UILabel、UIButton、UITextField文字下划线的设置方法(涉及到富文本的知识)
真是学到老活到老啊,梳理总结了一下关于富文本的知识,涉及到UILabel、UIButton、UITextField,好多比较经典的知识点,果断写出来,存起来。
一、UILabel
在实际开发中,经常会有几个文字或者一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求。通过NSMuttableAttstring(带属性的字符串),这些需求都可以很简便的实现。
使用字符串初始化
- (id)initWithString:(NSString *)
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:@&皮皮虾,我们走&];
- (id)initWithString:(NSString *)str attributes:(NSDictionary *)
字典中存放一些属性名和属性值,
NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:15.0],NSFontAttributeName,[UIColorredColor],NSForegroundColorAttributeName,NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@&皮皮虾,我们走& attributes:attributeDict];
- (id)initWithAttributedString:(NSAttributedString *)
使用NSAttributedString初始化,和NSMutableString、NSString类似
使用方法:
给某一范围内文字设置多个属性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)
给某一范围内文字添加某个属性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)
给某一范围内文字添加多个属性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)
移除某范围内的某个属性
- (void)removeAttribute:(NSString *)name range:(NSRange)
常见的属性及说明
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 =
[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 = mutableS
3.设置UILabel中指定内容的文字颜色
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@&今天天气很棒&];
[AttributedStr addAttribute:NSForegroundColorAttributeNamevalue:[UIColor redColor]range:NSMakeRange(2, 2)];
label.attributedText = AttributedS
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 = NSTextAlignmentC
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:titleforState: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里面重写父类方法
#import &UIKit/UIKit.h&
@interface UnderLiner : UITextField
#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));
步骤2:在使用它的地方,调用这个类,导入头文件,然后用UnderLiner创建使用
在需要使用的地方的.m文件里面的操作:
导入头文件: #import &UnderLiner.h&
具体使用方法:
- (void)initUI {
UnderLiner *textField = [[UnderLiner alloc] initWithFrame:CGRectMake(10, 450, 300, 45)];
textField.placeholder = @&请输入密码&;
textField.textAlignment = NSTextAlignmentL
[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 = UIViewContentModeC
pswTF.leftView = searchI
pswTF.leftViewMode = UITextFieldViewModeA
不再一一的把每个知识点的效果截图出来,直接截一个综合的效果图,看一下就可以啦。。。
<img alt="ios 关于UILabel、UIButton、UITextField文字下划线的设置方法(涉及到富文本的知识)" src="/d/file/p//510f9cdaab4.png"
以上就是ios 关于UILabel、UIButton、UITextField文字下划线的设置方法(涉及到富文本的知识)的全文介绍,希望对您学习和使用ios应用开发有所帮助.
这些内容可能对你也有帮助
更多可查看IOS开发列表页。
猜您也会喜欢这些文章IOS 开发之UILabel 或者 UIButton加下划线链接
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了IOS 开发之UILabel 或者 UIButton加下划线链接的相关资料,需要的朋友可以参考下
IOS 开发之UILabel 或者 UIButton加下划线链接
&&&&&&&& 本文主要介绍了IOS中 UILable及UIButton的带下划线链接的实现方法及附有源码下载,大家开发IOS 应用有需要的可以参考下:
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"查看所有中奖记录"];
NSRange strRange = {0,[str length]};
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
[_awardDisplayBtn setAttributedTitle:str forState:UIControlStateNormal];
HyperlinksButton.h
#import &UIKit/UIKit.h&
@interface HyperlinksButton : UIButton
UIColor *lineC
-(void)setColor:(UIColor*)
HyperlinksButton.m
[objc] view plain copy print&#63;
#import "HyperlinksButton.h"
@implementation HyperlinksButton
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self) {
-(void)setColor:(UIColor *)color{
lineColor = [color copy];
[self setNeedsDisplay];
- (void) drawRect:(CGRect)rect {
CGRect textRect = self.titleLabel.
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGFloat descender = self.titleLabel.font.
if([lineColor isKindOfClass:[UIColor class]]){
CGContextSetStrokeColorWithColor(contextRef, lineColor.CGColor);
CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender+1);
CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender+1);
CGContextClosePath(contextRef);
CGContextDrawPath(contextRef, kCGPathStroke);
直接将这个类 copy 到工程中,,然后将需要加下划线的 Button 类名改为 HyperlinksButton就可以了,提供了 setColor: 这个接口,可以设置下划线颜色,代码很简单,不解释了。UILabel 同理可得。
示例结果:
源码下载地址:/chaoyuan899/HyperlinksButton#how-to-use
本站源码下载:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具iOS 按钮上的文字添加下划线的方法_IOS
作者:用户
本文讲的是iOS 按钮上的文字添加下划线的方法_IOS,
问题:实现下图中右侧的按钮文字效果
[MyTools createMyImageview:topEditView frame:CGRectMake(widthAll-90, 12, 17, 16) imageName:@&离
问题:实现下图中右侧的按钮文字效果
[MyTools createMyImageview:topEditView frame:CGRectMake(widthAll-90, 12, 17, 16) imageName:@"离线课程_下载更多.png"];
UIButton *downMoreButton = [MyTools createMyBtn:@"下载更多" frame:CGRectMake(widthAll-70, 10, 60, 20) uiview:topEditView uifont:14 color:[UIColor colorWithRed:92/255.0 green:218/255.0 blue:231/255.0 alpha:1]];
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"下载更多"];
NSRange titleRange = {0,[title length]};
[title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
[downMoreButton setAttributedTitle:title
forState:UIControlStateNormal];
[downMoreButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
[downMoreButton addTarget:self action:@selector(downMore) forControlEvents:UIControlEventTouchUpInside];
关于iOS 按钮上的文字添加下划线的方法就给大家介绍到这里,希望对大家有所帮助!
以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索ios按钮下划线
ios文字下划线
ios 按钮文字下划线、ios 文字添加下划线、ios按钮下划线、ios 按钮加下划线、word文字添加下划线,以便于您获取更多的相关知识。
稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一
6款热门基础云产品6个月免费体验;2款产品1年体验;1款产品2年体验
弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率
开发者常用软件,超百款实用软件一站式提供
云栖社区()为您免费提供相关信息,包括
,所有相关内容均不代表云栖社区的意见!

我要回帖

更多关于 ios button 加下划线 的文章

 

随机推荐