uilabel 支持html,UILabel關於支持HTML格式 自動換行 行間距的封裝

在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 = [[NSMutableAttributedStringalloc] initWithString:text];

NSMutableParagraphStyle*paragraphStyle = [[NSMutableParagraphStylealloc] 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 = [[NSMutableAttributedStringalloc]initWithData:[textdataUsingEncoding:NSUnicodeStringEncoding]options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}documentAttributes:nilerror:nil];

NSMutableParagraphStyle*paragraphStyle = [[NSMutableParagraphStylealloc] 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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值