UILabel的使用
1.自动适配
2.设置label 的边距
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];style.alignment = NSTextAlignmentJustified;style.firstLineHeadIndent = 20.0f;style.headIndent = 10.0f;style.tailIndent = -10.0f;NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:@"设置边距设置边距设置边距设置边距" attributes:@{ NSParagraphStyleAttributeName : style}];UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 160, 200, 20)];label2.backgroundColor = [UIColor blueColor];label2.numberOfLines = 1;label2.attributedText = attrText;[self.view addSubview:label2];
3.关于限制最大宽度
设置最大宽度必须要设置为多行显示才起作用, 如果要显示单行可以设置一下高度为单行高度就可以了
UILabel* contentLabel = [[UILabel alloc]init];[self.view addSubview:contentLabel];contentLabel.numberOfLines = 0;contentLabel.backgroundColor = [UIColor blueColor];contentLabel.text = @"奔跑的兔子奔跑的兔子奔跑的兔子奔跑的兔子";//设置最大宽度必须要设置为多行显示才起作用, 如果要显示单行可以设置一下高度为单行高度就可以了contentLabel.preferredMaxLayoutWidth = 200;[contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(self.view).offset(20);make.top.mas_equalTo(self.view).offset(180);}];
4.富文本的使用
// 初始化NSMutableAttributedStringNSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];// 设置字体格式和大小NSString *str0 = @"设置字体格式和大小";NSDictionary *dictAttr0 = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};NSAttributedString *attr0 = [[NSAttributedString alloc] initWithString:str0attributes:dictAttr0];[attributedString appendAttributedString:attr0];// 设置字体颜色NSString *str1 = @"\n设置字体颜色\n";NSDictionary *dictAttr1 = @{NSForegroundColorAttributeName:[UIColor purpleColor]};NSAttributedString *attr1 = [[NSAttributedString alloc] initWithString:str1attributes:dictAttr1];[attributedString appendAttributedString:attr1];// 设置字体背景颜色NSString *str2 = @"设置字体背景颜色\n";NSDictionary *dictAttr2 = @{NSBackgroundColorAttributeName:[UIColor cyanColor]};NSAttributedString *attr2 = [[NSAttributedString alloc] initWithString:str2attributes:dictAttr2];[attributedString appendAttributedString:attr2];/*注:NSLigatureAttributeName设置连体属性,取值为NSNumber对象(整数),1表示使用默认的连体字符,0表示不使用,2表示使用所有连体符号(iOS不支持2)。而且并非所有的字符之间都有组合符合。如 fly ,f和l会连起来。*///设置连体属性NSString *str3 = @"fly";NSDictionary *dictAttr3 = @{NSFontAttributeName:[UIFont fontWithName:@"futura" size:14],NSLigatureAttributeName:[NSNumber numberWithInteger:1]};NSAttributedString *attr3 = [[NSAttributedString alloc]initWithString:str3attributes:dictAttr3];[attributedString appendAttributedString:attr3];/*!注:NSKernAttributeName用来设置字符之间的间距,取值为NSNumber对象(整数),负值间距变窄,正值间距变宽*/NSString *str4 = @"\n设置字符间距";NSDictionary *dictAttr4 = @{NSKernAttributeName:@(4)};NSAttributedString *attr4 = [[NSAttributedString alloc]initWithString:str4attributes:dictAttr4];[attributedString appendAttributedString:attr4];/*!注:NSStrikethroughStyleAttributeName设置删除线,取值为NSNumber对象,枚举NSUnderlineStyle中的值。NSStrikethroughColorAttributeName设置删除线的颜色。并可以将Style和Pattern相互 取与 获取不同的效果*/NSString *str51 = @"\n设置删除线为细单实线,颜色为红色";NSDictionary *dictAttr51 = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),NSStrikethroughColorAttributeName:[UIColor redColor]};NSAttributedString *attr51 = [[NSAttributedString alloc]initWithString:str51attributes:dictAttr51];[attributedString appendAttributedString:attr51];NSString *str52 = @"\n设置删除线为粗单实线,颜色为红色";NSDictionary *dictAttr52 = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick),NSStrikethroughColorAttributeName:[UIColor redColor]};NSAttributedString *attr52 = [[NSAttributedString alloc]initWithString:str52attributes:dictAttr52];[attributedString appendAttributedString:attr52];NSString *str53 = @"\n设置删除线为细单实线,颜色为红色";NSDictionary *dictAttr53 = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleDouble),NSStrikethroughColorAttributeName:[UIColor redColor]};NSAttributedString *attr53 = [[NSAttributedString alloc]initWithString:str53attributes:dictAttr53];[attributedString appendAttributedString:attr53];NSString *str54 = @"\n设置删除线为细单虚线,颜色为红色";NSDictionary *dictAttr54 = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternDot),NSStrikethroughColorAttributeName:[UIColor redColor]};NSAttributedString *attr54 = [[NSAttributedString alloc]initWithString:str54attributes:dictAttr54];[attributedString appendAttributedString:attr54];/*!NSStrokeWidthAttributeName 设置笔画的宽度,取值为NSNumber对象(整数),负值填充效果,正值是中空效果。NSStrokeColorAttributeName 设置填充部分颜色,取值为UIColor对象。设置中间部分颜色可以使用 NSForegroundColorAttributeName 属性来进行*///设置笔画宽度和填充部分颜色NSString *str6 = @"设置笔画宽度和填充颜色\n";NSDictionary *dictAttr6 = @{NSStrokeWidthAttributeName:@(2),NSStrokeColorAttributeName:[UIColor blueColor]};NSAttributedString *attr6 = [[NSAttributedString alloc]initWithString:str6attributes:dictAttr6];[attributedString appendAttributedString:attr6];//设置阴影属性,取值为NSShadow对象NSString *str7 = @"设置阴影属性\n";NSShadow *shadow = [[NSShadow alloc]init];shadow.shadowColor = [UIColor redColor];shadow.shadowBlurRadius = 1.0f;shadow.shadowOffset = CGSizeMake(1, 1);NSDictionary *dictAttr7 = @{NSShadowAttributeName:shadow};NSAttributedString *attr7 = [[NSAttributedString alloc]initWithString:str7attributes:dictAttr7];[attributedString appendAttributedString:attr7];//设置文本特殊效果,取值为NSString类型,目前只有一个可用效果 NSTextEffectLetterpressStyle(凸版印刷效果)
// NSString *str8 = @"设置特殊效果\n";
// NSDictionary *dictAttr8 = @{NSTextEffectAttributeName:NSTextEffectLetterpressStyle};
// NSAttributedString *attr8 = [[NSAttributedString alloc]initWithString:str8
// attributes:dictAttr8];
// [attributedString appendAttributedString:attr8];//设置文本附件,取值为NSTextAttachment对象,常用于文字的图文混排NSString *str9 = @"文字的图文混排\n";NSTextAttachment *textAttachment = [[NSTextAttachment alloc]init];textAttachment.image = [UIImage imageNamed:@"logo"];textAttachment.bounds = CGRectMake(0, 0, 30, 30);NSDictionary *dictAttr9 = @{NSAttachmentAttributeName:textAttachment};NSAttributedString *attr9 = [[NSAttributedString alloc]initWithString:str9attributes:dictAttr9];[attributedString appendAttributedString:attr9];/*!添加下划线 NSUnderlineStyleAttributeName。设置下划线的颜色 NSUnderlineColorAttributeName,对象为 UIColor。使用方式同删除线一样。*///添加下划线NSString *str10 = @"添加下划线\n";NSDictionary *dictAttr10 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSUnderlineColorAttributeName:[UIColor redColor]};NSAttributedString *attr10 = [[NSAttributedString alloc]initWithString:str10attributes:dictAttr10];[attributedString appendAttributedString:attr10];/*!NSBaselineOffsetAttributeName 设置基线偏移值。取值为NSNumber (float),正值上偏,负值下偏*///设置基线偏移值 NSBaselineOffsetAttributeNameNSString *str11 = @"添加基线偏移值\n";NSDictionary *dictAttr11 = @{NSBaselineOffsetAttributeName:@(-10)};NSAttributedString *attr11 = [[NSAttributedString alloc]initWithString:str11attributes:dictAttr11];[attributedString appendAttributedString:attr11];/*!NSObliquenessAttributeName 设置字体倾斜度,取值为 NSNumber(float),正值右倾,负值左倾*///设置字体倾斜度 NSObliquenessAttributeNameNSString *str12 = @"设置字体倾斜度\n";NSDictionary *dictAttr12 = @{NSObliquenessAttributeName:@(0.5)};NSAttributedString *attr12 = [[NSAttributedString alloc]initWithString:str12attributes:dictAttr12];[attributedString appendAttributedString:attr12];/*!NSExpansionAttributeName 设置字体的横向拉伸,取值为NSNumber (float),正值拉伸 ,负值压缩*///设置字体的横向拉伸 NSExpansionAttributeNameNSString *str13 = @"设置字体横向拉伸\n";NSDictionary *dictAttr13 = @{NSExpansionAttributeName:@(0.5)};NSAttributedString *attr13 = [[NSAttributedString alloc]initWithString:str13attributes:dictAttr13];[attributedString appendAttributedString:attr13];/*!NSWritingDirectionAttributeName 设置文字的书写方向,取值为以下组合@[@(NSWritingDirectionLeftToRight | NSWritingDirectionEmbedding)]@[@(NSWritingDirectionLeftToRight | NSWritingDirectionOverride)]@[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)]@[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)]???NSWritingDirectionEmbedding和NSWritingDirectionOverride有什么不同*///设置文字的书写方向 NSWritingDirectionAttributeNameNSString *str14 = @"设置文字书写方向\n";NSDictionary *dictAttr14 = @{NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)]};NSAttributedString *attr14 = [[NSAttributedString alloc]initWithString:str14attributes:dictAttr14];[attributedString appendAttributedString:attr14];/*!NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为NSNumber对象(整数),0表示横排文本,1表示竖排文本 在iOS中只支持0*///设置文字排版方向 NSVerticalGlyphFormAttributeNameNSString *str15 = @"设置文字排版方向\n";NSDictionary *dictAttr15 = @{NSVerticalGlyphFormAttributeName:@(0)};NSAttributedString *attr15 = [[NSAttributedString alloc]initWithString:str15attributes:dictAttr15];[attributedString appendAttributedString:attr15];//段落样式NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];//行间距paragraph.lineSpacing = 5;//段落间距paragraph.paragraphSpacing = 5;//对齐方式paragraph.alignment = NSTextAlignmentLeft;//指定段落开始的缩进像素paragraph.firstLineHeadIndent = 30;//调整全部文字的缩进像素paragraph.headIndent = 10;//添加段落设置[attributedString addAttribute:NSParagraphStyleAttributeNamevalue:paragraphrange:NSMakeRange(0, attributedString.length)];UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(60, 100, 300, 0)];label.backgroundColor = [UIColor lightGrayColor];//自动换行label.numberOfLines = 0;//设置label的富文本label.attributedText = attributedString;//label高度自适应[label sizeToFit];[self.view addSubview:label];
5,关于sizeThatFit与sizeToFit
sizeThatFit return 'best' size to fit given size. does not actually resize view. Default is return existing view size
sizeToFit calls sizeThatFits: with current view bounds and changes bounds size.
sizeThatFits 传CGSizeZero的话得出的size 是单行的高度,宽度为全显示字符宽度
sizeThatFits: 会计算出最优的 size 但是不会改变 自己的 size,而 sizeToFit: 会计算出最优的 size 而且会改变自己的 size。那么两者的联系是什么呢?
当调用 sizeToFit 后会调用 sizeThatFits 方法来计算 UIView 的 bounds.size 然后改变 frame.size。
如下代码testLabel2 的高度会自动调整
UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 50, 20)];testLabel1.text = @"欢迎关注黄飞的csdn博客,这里有你想要的东西,欢迎关注!!ggg";testLabel1.font = [UIFont systemFontOfSize:14];testLabel1.textAlignment=NSTextAlignmentCenter;testLabel1.numberOfLines = 0;//使用sizeThatFit计算lable大小CGSize sizeThatFit = [testLabel1 sizeThatFits:CGSizeZero]; //(426, 17)testLabel1.frame = CGRectMake(testLabel1.frame.origin.x, testLabel1.frame.origin.y, sizeThatFit.width, sizeThatFit.height);testLabel1.textColor=[UIColor blackColor];testLabel1.backgroundColor=[UIColor yellowColor];[self.view addSubview:testLabel1];UILabel *testLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, 100, 20)];testLabel2.text = @"欢迎关注黄飞的csdn博客,这里有你想要的东西,欢迎关注!!";testLabel2.font = [UIFont systemFontOfSize:14];testLabel2.textAlignment=NSTextAlignmentCenter;testLabel2.numberOfLines = 0;[testLabel2 sizeToFit];testLabel2.textColor=[UIColor blackColor];testLabel2.backgroundColor=[UIColor yellowColor];[self.view addSubview:testLabel2];
UILabel的使用相关推荐
- iostext添加点击事件_iOS给UILabel添加点击事件
前言:笔者最近需要实现给UILabel中的链接添加点击事件的功能.使用so.com查了下,发现TTTAttributedLabel的封装程度比较好.整理了TTTAttributedLabel的基本使用 ...
- iOS UILabel UITextView自适应文本,或文本大小自适应
//UILabel自适应文本的高度 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 300, 100)];label ...
- IOS UI 代码创建UIButton,UITextField,UILabel
//add a Label UILabel *label=[[UILabelalloc]initWithFrame:CGRectMake(100,20, 150, 40)]; [label setTe ...
- iOS 设置UILabel 的行间距
// // UILabel+LineSpace.h//#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface UILabel (L ...
- UILabel显示带颜色边的文字
需求如图,UILabel要实现带红色边的文字显示. 1.新建UILabel的子类JXBorderLabel 2.重写drawRect:方法 #import "JXBorderLabel.h& ...
- iOS 设置UILabel 的内边距
iOS 设置UILabel 的内边距 - (void)drawTextInRect:(CGRect)rect {UIEdgeInsets insets = {0, 5, 0, 5};[super dr ...
- 高逼格UILabel的闪烁动画效果
高逼格UILabel的闪烁动画效果 最终效果图如下: 源码: YXLabel.h 与 YXLabel.m // // YXLabel.h // // Created by YouXianMing o ...
- swift 动态设置UILabel的高度
2019独角兽企业重金招聘Python工程师标准>>> import UIKit class ViewController3: UIViewController {override ...
- IOS-开发日志-UILabel相关
UILabel属性 1.text:设置标签显示文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first"; NSM ...
- iOS UIlabel内容之后添加全文/展开
2019独角兽企业重金招聘Python工程师标准>>> 传入初始化frame之后的lab ,返回的是每行的string. 通过计算最后一行string的rect 得到全文/扩展btn ...
最新文章
- 如何使用 DBCC MEMORYSTATUS 命令来监视 SQL Server 2005 中的内存使用情况
- php判断值是否为空
- Android实现富文本时遇到的一些问题(2)字体效果的保存
- 【数据】深度学习从“数据集”开始
- 重构机房VB.NET机房收费系统个人重构版你都学会了什么(之一)
- 45. ExtJS ComboBox 下拉列表详细用法
- Javascript设计模式之中介者模式
- List中toArray()的使用方法
- windows 映射文件会释放内存吗_Windows系统共享内存管理
- 共享代码库,为何总被程序员弃用?
- SQL Server 和 Oracle 的常用函数对比
- C语言(二)C语言程序结构及简单的C程序举例
- 影响世界的100个管理定律
- R语言进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组四分位距(IQR)
- linux下载tkinter模块,Linux升级Python提示Tkinter模块找不到解决
- Viterbi-Algorithm(维特比)算法
- android wifi 框架图,android wifi框架
- 微信小程序电商商城系统怎样搭建?
- 动态规划(二)最优二分检索和0/1背包
- Docker镜像瘦身
热门文章
- 基于python语言的selenium自动化测试(1)-环境搭建
- Java第十六天~第十七天/11.18~11.19
- 高尔夫热潮四月优雅袭卷鹏城,深圳值得期待的运动类别盛会
- Hyperledger Fabric 1.0 快速搭建 -------- 多机部署 Fabric CA节点服务
- python 投屏_python实现《吃鸡大法》加文字识别 玩转百万英雄!
- amber分子动力学模拟干货总结
- 学生管理系统登入页面
- labview插件下载
- matlab中的将几条曲线画在一个坐标系下的方法,请问怎么将几条线画在同一个坐标轴下?有程序!...
- asp.net 的 web form 过时了吗?