直接將富文本轉(zhuǎn)成普通文本展示
//將Html字符串轉(zhuǎn)成OC的富文本對(duì)象
NSAttributedString *attStr = [[NSAttributedString alloc] initWithData:@"你想解析的html字符串" dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
//resultStr 就是我們要的普通字符串
NSString * resultStr=attStr.string;
通過(guò)原生控件的attributedText屬性展示
label.attributedText = attStr;
textField.attributedText=attStr;
textView.attributedText=attStr;
計(jì)算富文本的高度
+ (CGFloat)getStrHeightWithAttributeStr:(NSAttributedString *)string
viewWidth:(CGFloat)viewWidth{
if (string.length == 0) {
return 0;
}
CGSize size = [string boundingRectWithSize:CGSizeMake(viewWidth, MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
return ceil(size.height);
}
計(jì)算富文本的長(zhǎng)度
+ (CGFloat)getStrWidthWithAttributeStr:(NSAttributedString *)string
viewHeight:(CGFloat)viewHeight{
if (string.length == 0) {
return 0;
}
CGSize size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, viewHeight) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
return ceil(size.width);
}
UILabel實(shí)現(xiàn)點(diǎn)擊文字跳鏈效果(有相關(guān)需求的可以參照一下,編程思路應(yīng)該是一致的)
1.獲取用戶點(diǎn)擊事件
我這里是子類(lèi)化一個(gè)label,在其初始化的方法里添加了一個(gè)單擊手勢(shì)
@interface LSHtmlLabel ()
@property (nonatomic,strong)NSTextStorage *textStorage;
@property (nonatomic,strong)NSLayoutManager *layoutManager;
@property (nonatomic,strong)NSTextContainer *textContainer;
@end
@implementation LSHtmlLabel
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self)
{
self.userInteractionEnabled=YES;
self.numberOfLines=0;
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
[self addGestureRecognizer:tap];
self.textStorage = [NSTextStorage new];
self.layoutManager = [NSLayoutManager new];
self.textContainer = [NSTextContainer new];
[self.textStorage addLayoutManager:self.layoutManager];
[self.layoutManager addTextContainer:self.textContainer];
}
return self;
}
2.通過(guò)事件獲取用戶點(diǎn)擊的在UILabel上的位置,通過(guò)計(jì)算得出用戶點(diǎn)擊的文字及文字的下標(biāo)
-(void)tapClick:(UITapGestureRecognizer * )gesture
{
CGPoint location=[gesture locationInView:self];
self.textContainer.size = self.bounds.size;
self.textContainer.lineFragmentPadding = 0;
self.textContainer.maximumNumberOfLines = self.numberOfLines;
self.textContainer.lineBreakMode = self.lineBreakMode;
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
NSRange textRange = NSMakeRange(0, attributedText.length);
[attributedText addAttribute:NSFontAttributeName value:self.font range:textRange];
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = self.textAlignment;
[attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:textRange];
[self.textStorage setAttributedString:attributedText];
CGSize textSize = [self.layoutManager usedRectForTextContainer:self.textContainer].size;
// location.x -= (CGRectGetWidth(self.label.frame) - textSize.width) / 2;
location.y -= (CGRectGetHeight(self.frame) - textSize.height) / 2;
NSUInteger glyphIndex = [self.layoutManager glyphIndexForPoint:location inTextContainer:self.textContainer];
CGFloat fontPointSize = self.font.pointSize;
[self.layoutManager setAttachmentSize:CGSizeMake(fontPointSize, fontPointSize) forGlyphRange:NSMakeRange(self.text.length - 1, 1)];
NSAttributedString *attributedSubstring = [self.attributedText attributedSubstringFromRange:NSMakeRange(glyphIndex, 1)];
CGRect glyphRect = [self.layoutManager boundingRectForGlyphRange:NSMakeRange(glyphIndex, 1)
inTextContainer:self.textContainer];
if (!CGRectContainsPoint(glyphRect, location)) {
if (CGRectContainsPoint(CGRectMake(0, 0, textSize.width, textSize.height), location)) {
}
NSLog(@"沒(méi)找到呢,該怎么辦才好呢");
return;
}
//此處我通過(guò)block塊返回的兩個(gè)值
_clickLabelBlock(self,(unsigned long)glyphIndex,attributedSubstring);
}
3.拿到用戶點(diǎn)擊的文字下標(biāo),富文本文字此時(shí)你就可以為所欲為啦
LSHtmlLabel * label=[[LSHtmlLabel alloc]initWithFrame:_htmlLabel.frame];
[label setClickLabelBlock:^(LSHtmlLabel * _Nonnull label, NSUInteger index, NSAttributedString * _Nonnull attributedSubstring) {
NSLog(@"%@",attributedSubstring);
//獲取文字屬性字典
NSRange range = NSMakeRange(0, 1);
NSDictionary*dic = [attributedSubstring attributesAtIndex:0 effectiveRange:&range];
//判斷超鏈接是否存在 添加對(duì)應(yīng)的處理邏輯
if (dic[@"NSLink"])
{
NSLog(@"%@",dic[@"NSLink"]);
}
}];