UILabel attributedText的一个坑
2016-07-08 16:44
357 查看
写一个自动计算高度控制行间距的UILabel
先写个UILabel
- (UILabel *)resultLbl{
if (_resultLbl ==
nil) {
_resultLbl = [[UILabel
alloc] init];
_resultLbl.backgroundColor =[UIColor
whiteColor];
_resultLbl.numberOfLines =
0;
_resultLbl.textColor = [UIColor
settingRightTitleColor];
_resultLbl.font = [UIFont
systemFontOfSize:RejectResult_resultlbl_fint];
_resultLbl.textAlignment =
NSTextAlignmentCenter;
_resultLbl.frame =
CGRectMake(15,0,
ScreenWidth -
30,
self.resultCellHeight);
}
return
_resultLbl;
}
用到了富文本的设置
- (NSAttributedString *)setLabelLineSpace:(CGFloat)lineSpace text:(NSString *)text{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc]
initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc]
init];
[paragraphStyle setLineSpacing:lineSpace];//调整行间距
return attributedString;
}
然后我就开始计算了
- (CGFloat)sizeWithFont:(UIFont *)font constrainedTosize:(CGSize)size lineSpace:(CGFloat)lineSpace text:(NSString
*)text{
CGFloat oneRowHeihgt = [@"test"
sizeWithAttributes:@{NSFontAttributeName:font}].height;
CGSize textSize = [text
boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:font}
context:nil].size;
//计算出真实的大小
CGFloat rows = textSize.height / oneRowHeihgt;
CGFloat realHeight = (rows *
ceilf(oneRowHeihgt)) + (rows - 1)* lineSpace;
return realHeight;
}
然后我发现我怎么设置文本显示出来都不是居中了 竟然是左对齐 我的个去类!!!啥情况,然后开始找原因
原因是 在设置attributedText富文本的时候将忽略下列基础设置
@property(nullable,
nonatomic,copy)
NSString *text;
// default is nil
@property(null_resettable,
nonatomic,strong)
UIFont *font;
// default is nil (system font 17 plain)
@property(null_resettable,
nonatomic,strong)
UIColor *textColor;
// default is nil (text draws black)
@property(nullable,
nonatomic,strong)
UIColor *shadowColor;
// default is nil (no shadow)
@property(nonatomic)
CGSize shadowOffset;
// default is CGSizeMake(0, -1) -- a top shadow
@property(nonatomic)
NSTextAlignment textAlignment;
// default is NSTextAlignmentLeft
@property(nonatomic)
NSLineBreakMode lineBreakMode;
// default is NSLineBreakByTruncatingTail. used for single and multiple lines of text
文档才是硬道理呀 !!!!!
解决办法是 在函数中恢复设置 。。。。。。。。。代码直接改下见谅。。。
- (NSAttributedString *)setLabelLineSpace:(CGFloat)lineSpace text:(NSString *)text{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpace];//调整行间距
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont
systemFontOfSize:RejectResult_resultlbl_fint]
range:NSMakeRange(0, [text
length])];
[attributedString addAttribute:NSBackgroundColorAttributeName
value:[UIColor
whiteColor] range:NSMakeRange(0, [text
length])];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor
settingRightTitleColor]
range:NSMakeRange(0, [text
length])];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [text
length])];
return attributedString;
}
//练手记录、
使用方法:
为某一范围内文字设置多个属性
- (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;
2. 常见的属性及说明
NSFontAttributeName 字体
NSParagraphStyleAttributeName 段落格式
NSForegroundColorAttributeName 字体颜色
NSBackgroundColorAttributeName 背景颜色
NSStrikethroughStyleAttributeName删除线格式
NSUnderlineStyleAttributeName 下划线格式
NSStrokeColorAttributeName 删除线颜色
NSStrokeWidthAttributeName删除线宽度
NSShadowAttributeName 阴影
参考资料:http://blog.csdn.net/reylen/article/details/41208747
在送一个很好的总结 :https://segmentfault.com/a/1190000003491677
上面博主真心大爱。。。。。崇拜 啥时候我能赶上那么一点点呢 ~_~
先写个UILabel
- (UILabel *)resultLbl{
if (_resultLbl ==
nil) {
_resultLbl = [[UILabel
alloc] init];
_resultLbl.backgroundColor =[UIColor
whiteColor];
_resultLbl.numberOfLines =
0;
_resultLbl.textColor = [UIColor
settingRightTitleColor];
_resultLbl.font = [UIFont
systemFontOfSize:RejectResult_resultlbl_fint];
_resultLbl.textAlignment =
NSTextAlignmentCenter;
_resultLbl.frame =
CGRectMake(15,0,
ScreenWidth -
30,
self.resultCellHeight);
}
return
_resultLbl;
}
用到了富文本的设置
- (NSAttributedString *)setLabelLineSpace:(CGFloat)lineSpace text:(NSString *)text{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc]
initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc]
init];
[paragraphStyle setLineSpacing:lineSpace];//调整行间距
return attributedString;
}
然后我就开始计算了
- (CGFloat)sizeWithFont:(UIFont *)font constrainedTosize:(CGSize)size lineSpace:(CGFloat)lineSpace text:(NSString
*)text{
CGFloat oneRowHeihgt = [@"test"
sizeWithAttributes:@{NSFontAttributeName:font}].height;
CGSize textSize = [text
boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:font}
context:nil].size;
//计算出真实的大小
CGFloat rows = textSize.height / oneRowHeihgt;
CGFloat realHeight = (rows *
ceilf(oneRowHeihgt)) + (rows - 1)* lineSpace;
return realHeight;
}
然后我发现我怎么设置文本显示出来都不是居中了 竟然是左对齐 我的个去类!!!啥情况,然后开始找原因
原因是 在设置attributedText富文本的时候将忽略下列基础设置
@property(nullable,
nonatomic,copy)
NSString *text;
// default is nil
@property(null_resettable,
nonatomic,strong)
UIFont *font;
// default is nil (system font 17 plain)
@property(null_resettable,
nonatomic,strong)
UIColor *textColor;
// default is nil (text draws black)
@property(nullable,
nonatomic,strong)
UIColor *shadowColor;
// default is nil (no shadow)
@property(nonatomic)
CGSize shadowOffset;
// default is CGSizeMake(0, -1) -- a top shadow
@property(nonatomic)
NSTextAlignment textAlignment;
// default is NSTextAlignmentLeft
@property(nonatomic)
NSLineBreakMode lineBreakMode;
// default is NSLineBreakByTruncatingTail. used for single and multiple lines of text
文档才是硬道理呀 !!!!!
解决办法是 在函数中恢复设置 。。。。。。。。。代码直接改下见谅。。。
- (NSAttributedString *)setLabelLineSpace:(CGFloat)lineSpace text:(NSString *)text{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpace];//调整行间距
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont
systemFontOfSize:RejectResult_resultlbl_fint]
range:NSMakeRange(0, [text
length])];
[attributedString addAttribute:NSBackgroundColorAttributeName
value:[UIColor
whiteColor] range:NSMakeRange(0, [text
length])];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor
settingRightTitleColor]
range:NSMakeRange(0, [text
length])];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [text
length])];
return attributedString;
}
//练手记录、
使用方法:
为某一范围内文字设置多个属性
- (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;
2. 常见的属性及说明
NSFontAttributeName 字体
NSParagraphStyleAttributeName 段落格式
NSForegroundColorAttributeName 字体颜色
NSBackgroundColorAttributeName 背景颜色
NSStrikethroughStyleAttributeName删除线格式
NSUnderlineStyleAttributeName 下划线格式
NSStrokeColorAttributeName 删除线颜色
NSStrokeWidthAttributeName删除线宽度
NSShadowAttributeName 阴影
参考资料:http://blog.csdn.net/reylen/article/details/41208747
在送一个很好的总结 :https://segmentfault.com/a/1190000003491677
上面博主真心大爱。。。。。崇拜 啥时候我能赶上那么一点点呢 ~_~
相关文章推荐
- easyui
- easyui的验证
- 浅谈Java设计模式(四)建造者模式(Builder)
- <java.util>PriorityQueue代码分析
- Angular-Ui-Router+ocLazyLoad动态加载脚本
- easyui--datetimebox插件,下拉框的日期不能全部显示,即一个月份中的天数未全部显示
- android测试详解_2-Building Local Unit Tests-本地单元测试
- easyui中datetimebox不显示日期框
- UVA - 10420 List of Conquests
- Codeforces 689 C The Values You Can Make(dp)
- CharSequence 只要要求是一串字符就行了可以有StringBuffer和StringBuilder
- Rescue 优先队列+广搜
- GO中url.Values作为参数传递的两种解析方法
- HDU 1242 Rescue【BFS+优先队列】
- UIView的clipsToBounds属性和CALayer的masksToBounds属性的比较
- UIView的clipsToBounds属性和CALayer的masksToBounds属性的比较
- 3D UI场景中如何把XY平面的尺寸映射为屏幕像素
- JS中变量名作为if条件的 true/flase
- 虚拟现实-VR-UE4-编辑自定义Character-上下左右移动-旋转
- POJ2299 Ultra-QuickSor[树状数组+离散化 / 归并排序]