UILabel关于支持HTML格式 自动换行 行间距的封装
2016-01-08 10:44
567 查看
在APP开发中,会经常出现大段的文字,为了排版的好看需要进行排版,现在封装了两个方法,分别输入字符串,字体,字体颜色,行间距和控件的大小,即可返回一个UILabel,支持自动折行,第一个方法输入的是普通的字符串,第二个方法输入的是HTML字符串.代码如下:
/* 普通字符串 */
- (UILabel *)returnLineSpacingLabelWithText:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor lineSpace:(float)lineSpace
frame:(CGRect)frame
{
UILabel *label = [[UILabel
alloc] initWithFrame:frame];
label.font = font;
label.textColor = textColor;
label.textAlignment =
NSTextAlignmentCenter;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc]
initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc]
init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [text
length])];
label.attributedText = attributedString;
label.numberOfLines =
0;
[label sizeToFit];
return label;
}
/* HTML字符串 */
- (UILabel *)returnLineSpacingLabelWithHtmlText:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor lineSpace:(float)lineSpace
frame:(CGRect)frame
{
UILabel *label = [[UILabel
alloc] initWithFrame:frame];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString
alloc] initWithData:[text
dataUsingEncoding:NSUnicodeStringEncoding]
options:@{
NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType
} documentAttributes:nil
error:nil];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc]
init];
[paragraphStyle setLineSpacing:lineSpace];
[attrStr addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [attrStr
length])];
label.attributedText = attrStr;
label.font = font;
label.textColor = textColor;
label.numberOfLines =
0;
[label sizeToFit];
return label;
}
/* 普通字符串 */
- (UILabel *)returnLineSpacingLabelWithText:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor lineSpace:(float)lineSpace
frame:(CGRect)frame
{
UILabel *label = [[UILabel
alloc] initWithFrame:frame];
label.font = font;
label.textColor = textColor;
label.textAlignment =
NSTextAlignmentCenter;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc]
initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc]
init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [text
length])];
label.attributedText = attributedString;
label.numberOfLines =
0;
[label sizeToFit];
return label;
}
/* HTML字符串 */
- (UILabel *)returnLineSpacingLabelWithHtmlText:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor lineSpace:(float)lineSpace
frame:(CGRect)frame
{
UILabel *label = [[UILabel
alloc] initWithFrame:frame];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString
alloc] initWithData:[text
dataUsingEncoding:NSUnicodeStringEncoding]
options:@{
NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType
} documentAttributes:nil
error:nil];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc]
init];
[paragraphStyle setLineSpacing:lineSpace];
[attrStr addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [attrStr
length])];
label.attributedText = attrStr;
label.font = font;
label.textColor = textColor;
label.numberOfLines =
0;
[label sizeToFit];
return label;
}
相关文章推荐
- GUI - Web前端开发框架
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 评价ui设计作品好坏的八个标准(界面/交互设计研究)
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 在Windows 8.1的IE 11中屏蔽双击放大功能
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- flash 系统字体显示问题
- 通过Mootools 1.2来操纵HTML DOM元素
- WEB标准网页布局中尽量不要使用的HTML标签
- Flash 与 html 的一些实用技巧
- html工作中表格<tbody>标签的使用技巧
- HTML 向 XHTML1.0 兼容性指导
- C#及WPF获取本机所有字体和颜色的方法
- C#自写的一个HTML解析类(类似XElement语法)
- 谈谈网页设计中的字体应用Font Set