textView加載帶大量html標(biāo)簽的文本剥险,并實(shí)現(xiàn)點(diǎn)擊圖片預(yù)覽

測(cè)試文本數(shù)據(jù)


//html標(biāo)簽圖片自適應(yīng)

NSString *detailTextString = [STNewsDetailManager htmlImageHeighAutoStr:string width:self.textView.width];

+(NSString*)htmlImageHeighAutoStr:(NSString*)htmlStr width:(CGFloat)width

{

? ? NSString*detailTextString =@"";

//? ? 根據(jù)圖片大小自適應(yīng)抬旺,圖片寬度小于width蟀俊,圖片不變旗扑,寬度大于width捎谨,自適應(yīng)到屏幕寬度

? ? if(![WDUtilisNullOrEmpty:htmlStr]) {

? ? ? ? detailTextString = [NSString stringWithFormat:@"img{max-width:%f !important;height:auto}%@",width,htmlStr];

? ? }

? ? returndetailTextString;

}


?//轉(zhuǎn)換成AttributedString提取圖片URL

? ? NSMutableAttributedString *firstPart = [[NSMutableAttributedString alloc] initWithString:detailText];

? ? [self getImageurlFromHtml:firstPart];

//手動(dòng)提取文本中的圖片URL

- (NSArray*) getImageurlFromHtml:(NSMutableAttributedString*) webString

{

? ? if(webString.length==0) {

? ? ? ? returnnil;

? ? }

? ? NSString*webStr? = [NSString stringWithFormat:@"%@",webString];

? ? self.imageurlArray = [NSMutableArray arrayWithCapacity:1];

? ? //標(biāo)簽匹配

? ? NSString*parten =@"<img(.*?)>";

? ? NSError* error =NULL;

? ? NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:parten options:0 error:&error];

? ? NSArray* match = [reg matchesInString:webStr options:0 range:NSMakeRange(0, [webString length] - 1)];

? ? for(NSTextCheckingResult* resultinmatch) {

?? ? ? ?//過(guò)去數(shù)組中的標(biāo)簽

? ? ? ? NSRangerange = [resultrange];

? ? ? ? NSString* subString = [webStrsubstringWithRange:range];

?? ? ? ?//從圖片中的標(biāo)簽中提取ImageURL

? ? ? ? NSRegularExpression *subReg = [NSRegularExpression regularExpressionWithPattern:@"http://(.*?)\"" options:0 error:NULL];

? ? ? ? NSArray* match = [subRegmatchesInString:subStringoptions:0range:NSMakeRange(0, [subStringlength] -1)];

? ? ? ? if(match.count>0) {

? ? ? ? ? ? NSTextCheckingResult* subRes = match[0];

? ? ? ? ? ? NSRangesubRange = [subResrange];

? ? ? ? ? ? subRange.length= subRange.length-1;

? ? ? ? ? ? NSString* imagekUrl = [subStringsubstringWithRange:subRange];

? ? ? ? }

? ? ? ? else{

? ? ? ? ? ? NSRegularExpression *subReg = [NSRegularExpression regularExpressionWithPattern:@"https://(.*?)\"" options:0 error:NULL];

? ? ? ? ? ? NSArray* match = [subRegmatchesInString:subStringoptions:0range:NSMakeRange(0, [subStringlength] -1)];

? ? ? ? ? ? if(match.count>0) {

? ? ? ? ? ? ? ? NSTextCheckingResult* subRes = match[0];

? ? ? ? ? ? ? ? NSRangesubRange = [subResrange];

? ? ? ? ? ? ? ? subRange.length= subRange.length-1;

? ? ? ? ? ? ? ? NSString* imagekUrl = [subStringsubstringWithRange:subRange];

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? return self.imageurlArray;

}


//給textView賦值

self.textView.asyncAttributStr = detailTextString;

//加載完成后的回調(diào)

__weak typeof (self) weakSelf = self;

? ? ?self.textView.attributeBlock = ^{

//找到每個(gè)圖片所在的位置

[weakSelf.textView.attributedText enumerateAttributesInRange:NSMakeRange(0,weakSelf.textView.attributedText.length) options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {

? ? ? ? ? ? if(attrs[@"NSAttachment"]) {

? ? ? ? ? ? ? ? NSTextAttachment*attachment = attrs[@"NSAttachment"];

? ? ? ? ? ? ? ? if([attachment.fileTypecontainsString:@"png"] ||

? ? ? ? ? ? ? ? ? ? [attachment.fileTypecontainsString:@"jpg"] ||

? ? ? ? ? ? ? ? ? ? [attachment.fileTypecontainsString:@"jpeg"]||

? ? ? ? ? ? ? ? ? ? [attachment.fileTypecontainsString:@"bmp"]||

? ? ? ? ? ? ? ? ? ? [attachment.fileTypecontainsString:@"gif"]) {

? ? ? ? ? ? ? ? ? ? NSString*str = [NSStringstringWithFormat:@"%ld",(unsignedlong)range.location]

? ? ? ? ? ? ? ? ? ? [weakSelf.rangeArrinsertObject:stratIndex:0];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }];

};


//點(diǎn)擊圖片出發(fā)的代理方法(ios11.1系統(tǒng)的手機(jī)觸發(fā)需要點(diǎn)擊事件稍長(zhǎng)诫尽,此問(wèn)題禀酱,沒(méi)有找到解決辦法,感覺(jué)是蘋(píng)果系統(tǒng)系統(tǒng)問(wèn)題)

- (BOOL)textView:(UITextView*)textView shouldInteractWithTextAttachment:(NSTextAttachment*)textAttachment inRange:(NSRange)characterRange{

? ? NSLog(@"點(diǎn)擊的第%lu個(gè)",(unsignedlong)characterRange.location);

? ? NSLog(@"圖片共%lu個(gè)",(unsignedlong)self.imageurlArray.count);

? ? NSString*location = [NSStringstringWithFormat:@"%ld",(unsignedlong)characterRange.location];

? ? if(self.rangeArr.count>0) {

? ? ? ? for(NSIntegeri =0; i

? ? ? ? ? ? NSString*str = [self.rangeArrobjectAtIndex:i];

? ? ? ? ? ? if([strisEqualToString:location] &&self.imageurlArray.count>=i+1) {

? ? ? ? ? ? ? ? if(self.imageBlock) {

? ? ?//根據(jù)自己的情況牧嫉,跳轉(zhuǎn)到圖片瀏覽頁(yè)面

? ? ? ? ? ? ? ? ? ? self.imageBlock(self,i,self.imageurlArray);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? return YES;

}


textView的初始化

- (STCustomTextView*)textView{?

? ? if(!_textView) {

? ? ? ? STCustomTextView *textView = [[STCustomTextView alloc] init];

? ? ? ? textView.myFont = [UIFont extFontOfSize:self.currentFontSize];

? ? ? ? textView.isAdjustHeightByWidth = YES;

? ? ? ? textView.lineRowSpacing=7;//行間距

? ? ? ? textView.lineAfterParagraphSpacing = 15;//段后間距

? ? ? ? textView.delegate=self;

? ? ? ? textView.backgroundColor = [ThemeManager _setupDayColor:nil nightColor:ST_Night_BackGroundColor];

? ? ? ? textView.myColor = [ThemeManager _setupDayColor:[WDUtil colorWithHexString:@"#181818"] nightColor:ST_Night_BigTitleColor];

? ? ? ? textView.editable=NO;? ? ? ? //必須禁止輸入剂跟,否則點(diǎn)擊將彈出輸入鍵盤(pán)

? ? ? ? textView.scrollEnabled=NO;

? ? ? ? _textView= textView;

? ? }

? ? return _textView;

}


自定義STCustomTextView

原本使用的是label,由于要加上點(diǎn)擊圖片瀏覽功能酣藻,所以換成了TextView曹洽,里面有些不用的代碼就給注釋了

.m文件

#import "STCustomTextView.h"

@implementationSTCustomTextView

-(instancetype)initWithFrame:(CGRect)frame

{

? ? self= [superinitWithFrame:frame];

? ? if(self) {

? ? ? ? //? ? ? ? _myLineBreakMode = NSLineBreakByWordWrapping;

//? ? ? ? _myAlignment = kMyTextAlignmentLeft;

? ? ? ? _isAdjustHeightByWidth = NO;

? ? ? ? _isAdjustWidthByHeight = NO;

? ? ? ? _isDestroyAttributeBlockAfterUse = NO;

? ? ? ? _lineRowSpacing = -1; //行間距

? ? ? ? _lineAfterParagraphSpacing = -1; //段前間距

? ? ? ? _lineBeforeParagraphSpacing = -1; //段后間距

? ? }

? ? return self;

}

#pragma mark - Set methods

#pragma mark 同步加載富文本 attributStr

-(void)setAttributStr:(NSString*)attributStr

{

? ? //? ? detailsModel.mainBody = @"測(cè)試";

? ? _attributStr= attributStr;

? ? self.attributedText= [selfaddMessageAttribute:attributStr];

? ? [self reloadAttributeData];

}

#pragma mark 異步加載富文本 asyncAttributStr

-(void)setAsyncAttributStr:(NSString*)asyncAttributStr

{

? ? _asyncAttributStr= asyncAttributStr;

? ? self.text=@"";//先清除文字再加載

? ? __weaktypeof(self) weakSelf =self;


? ? dispatch_async(dispatch_get_global_queue(0, 0), ^{

? ? ? ? NSMutableAttributedString*firstPart = [selfaddMessageAttribute:asyncAttributStr];

? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? weakSelf.attributedText= firstPart;

? ? ? ? ? ? [weakSelfreloadAttributeData];

? ? ? ? ? ? [weakSelfinvocationAttributeBlock];

? ? ? ? });

? ? });

}

//編輯NSMutableAttributedString

-(NSMutableAttributedString*)addMessageAttribute:(NSString*)message

{

? ? //? ? message = [message stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%f.00pt",self.font.pointSize] withString:@"12.00pt"];

? ? //設(shè)置文字大小

? ? message = [NSString stringWithFormat:@"%@%@",self.font.pointSize,message,@""];

? ? NSMutableAttributedString *firstPart = [[NSMutableAttributedString alloc] initWithString:@""];

? ? @try{

? ? ? ? //空字符串直接返回nil

? ? ? ? if([selfisMessageNullOrEmpty:message]) {

? ? ? ? ? ? returnfirstPart;

? ? ? ? }

? ? ? ? firstPart = [[NSMutableAttributedString alloc]

?? ? ? ? ? ? ? ? ? ? initWithData:[message dataUsingEncoding:NSUnicodeStringEncoding]

?? ? ? ? ? ? ? ? ? ? options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}

?? ? ? ? ? ? ? ? ? ? documentAttributes:nil

?? ? ? ? ? ? ? ? ? ? error:nil];


? ? ? ? //? ? ? ? //文字大小

? ? ? ? //? ? ? ? [firstPart addAttribute:NSFontAttributeName

? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? value:self.font

? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, firstPart.length)];

? ? ? ? [firstPartaddAttribute:NSKernAttributeName

? ? ? ? ? ? ? ? ? ? ? ? ? value:@1.0f

? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, firstPart.length)];

? ? ? ? //沒(méi)有color,設(shè)置默認(rèn)顏色

? ? ? ? //? ? ? ? if (self.myColor && [self isNotHaveColorInString:message])

? ? ? ? if ([ThemeManager sharedThemeManager].isNight) {

? ? ? ? ? ? if(self.myColor){

? ? ? ? ? ? ? ? [firstPartaddAttribute:NSForegroundColorAttributeName

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? value:self.myColor

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, firstPart.length)];

? ? ? ? ? ? }

? ? ? ? }else{

? ? ? ? ? ? if(self.myColor&& [selfisNotHaveColorInString:message]){

? ? ? ? ? ? ? ? [firstPartaddAttribute:NSForegroundColorAttributeName

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? value:self.myColor

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, firstPart.length)];

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? if(self.lineRowSpacing>=0

? ? ? ? ? ? ||self.lineAfterParagraphSpacing >= 0

? ? ? ? ? ? ||self.lineBeforeParagraphSpacing >= 0)

? ? ? ? {

? ? ? ? ? ? NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc] init];

? ? ? ? ? ? if(self.lineRowSpacing>=0? ? ) {

? ? ? ? ? ? ? ? paragraph.lineSpacing=self.lineRowSpacing;

? ? ? ? ? ? }

? ? ? ? ? ? if (self.lineAfterParagraphSpacing >= 0) {

? ? ? ? ? ? ? ? paragraph.paragraphSpacing=self.lineAfterParagraphSpacing;

? ? ? ? ? ? }

? ? ? ? ? ? if (self.lineBeforeParagraphSpacing >= 0) {

? ? ? ? ? ? ? ? paragraph.paragraphSpacingBefore=self.lineBeforeParagraphSpacing;

? ? ? ? ? ? }


? ? ? ? ? ? [firstPartaddAttribute:NSParagraphStyleAttributeName

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? value:paragraph

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, firstPart.length)];

? ? ? ? }

? ? ? ? returnfirstPart;

? ? }

? ? @catch(NSException *exception) {

? ? ? ? returnfirstPart;

? ? }

}

//更新界面

-(void)reloadAttributeData

{

//? ? [self customAttributeAlignment:self.myAlignment];

? ? //? ? self.lineBreakMode = self.myLineBreakMode;

? ? //重新布局

? ? [self layoutAdaptiveLable];

}

#pragma mark 更新自適應(yīng)大小

- (void)layoutAdaptiveLable

{

? ? if (self.isAdjustWidthByHeight == NO && self.isAdjustHeightByWidth == NO) {

? ? ? ? return;

? ? }

//? ? self.numberOfLines = 0;

? ? CGRectnewFrame =self.frame;

? ? if (self.isAdjustHeightByWidth == YES) {? //高度自適應(yīng)

? ? ? ? CGFloat height = [self sizeThatFits:CGSizeMake(self.frame.size.width, MAXFLOAT)].height;

? ? ? ? newFrame.size.height= height;


? ? }else if (self.isAdjustWidthByHeight == YES){ //換掉自適應(yīng)

? ? ? ? CGFloat width = [self sizeThatFits:CGSizeMake(MAXFLOAT,self.frame.size.width)].width;

? ? ? ? newFrame.size.width= width;

? ? }

? ? self.frame= newFrame;

}

-(void)invocationAttributeBlock

{

? ? if (self.attributeBlock) {

? ? ? ? self.attributeBlock();

? ? ? ? if (self.isDestroyAttributeBlockAfterUse) {

? ? ? ? ? ? self.attributeBlock=nil;

? ? ? ? }

? ? }

}

//string中是否有color標(biāo)簽辽剧,myColor為nil送淆,不設(shè)置默認(rèn)顏色

//NO:表示有Color,不設(shè)置默認(rèn)顏色怕轿; YES偷崩,表示沒(méi)有color,可以設(shè)置默認(rèn)顏色

-(BOOL)isNotHaveColorInString:(NSString*)string

{

? ? if (self.myColor) { //myColor為nil撤卢,不設(shè)置默認(rèn)顏色

? ? ? ? NSRangerange = [stringrangeOfString:@"color"];

? ? ? ? if(range.location==NSNotFound) {

? ? ? ? ? ? returnYES;

? ? ? ? }

? ? }

? ? return NO;

}

#pragma mark myAttributedText

//重置富文本

-(void)setMyAttributedText:(NSAttributedString*)myAttributedText

{

? ? _myAttributedText= myAttributedText;

? ? self.attributedText= myAttributedText;

? ? [self reloadAttributeData];

}

#pragma mark 居中方式

//-(void)setMyAlignment:(MyCustomStrAlignment)myAlignment

//{

//? ? _myAlignment = myAlignment;

//? ? [self customAttributeAlignment:myAlignment];

//}

#pragma mark 字體大小

-(void)setMyFont:(UIFont*)myFont

{

? ? _myFont= myFont;

? ? if(myFont) {

? ? ? ? self.font= myFont;

? ? }

? ? //字體大小改變,界面布局也要變化

? ? [self layoutAdaptiveLable];

}

#pragma mark 顏色

-(void)setMyColor:(UIColor*)myColor

{

? ? _myColor= myColor;

? ? self.textColor= myColor;

}

#pragma mark 文字格式

//-(void)setMyLineBreakMode:(NSLineBreakMode)myLineBreakMode

//{

//? ? _myLineBreakMode = myLineBreakMode;

//? ? self.lineBreakMode = myLineBreakMode;

//}

#pragma mark *行間距 段前間距 段后間距

/*行間距**/

-(void)setLineRowSpacing:(CGFloat)lineRowSpacing

{

? ? _lineRowSpacing= lineRowSpacing;

}

/**段前間距**/

-(void)setLineBeforeParagraphSpacing:(CGFloat)lineBeforeParagraphSpacing

{

? ? _lineBeforeParagraphSpacing= lineBeforeParagraphSpacing;

}

/**段后間距**/

-(void)setLineAfterParagraphSpacing:(CGFloat)lineAfterParagraphSpacing

{

? ? _lineAfterParagraphSpacing= lineAfterParagraphSpacing;

}

#pragma mark 是否允許適配高度/寬度

/**固定寬度適配高度 默認(rèn) NO**/

-(void)setIsAdjustHeightByWidth:(BOOL)isAdaptiveHeightByWidth

{

? ? _isAdjustHeightByWidth= isAdaptiveHeightByWidth;


? ? if(isAdaptiveHeightByWidth) {

? ? ? ? _isAdjustWidthByHeight = NO;

? ? }

}

/**固定高度適配寬度 默認(rèn) NO**/

-(void)setIsAdjustWidthByHeight:(BOOL)isAdaptiveWidthByHeight

{

? ? _isAdjustWidthByHeight= isAdaptiveWidthByHeight;

? ? if(isAdaptiveWidthByHeight) {

? ? ? ? _isAdjustHeightByWidth = NO;

? ? }

}

#pragma mark - Public methods

//居中方式

//-(void)customAttributeAlignment:(MyCustomStrAlignment )myAlignment

//{

//? ? switch (myAlignment) {

//? ? ? ? case kMyTextAlignmentCenter:{

//? ? ? ? ? ? self.textAlignment = NSTextAlignmentCenter;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case kMyTextAlignmentLeft:{

//? ? ? ? ? ? self.textAlignment = NSTextAlignmentLeft;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case kMyTextAlignmentJustified:{

//? ? ? ? ? ? self.textAlignment = NSTextAlignmentJustified;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case kMyTextAlignmentNatural:{

//? ? ? ? ? ? self.textAlignment = NSTextAlignmentNatural;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case kMyTextAlignmentRight:

//? ? ? ? {

//? ? ? ? ? ? self.textAlignment = NSTextAlignmentRight;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? default:

//? ? ? ? ? ? break;

//? ? }

//? ?

//}

#pragma mark - Private methods

//將自定義MyCustomStrAlignment 換成 NSTextAlignment

//+(NSTextAlignment)changeCustomAlignment:(MyCustomStrAlignment)myAlignment

//{

//? ? NSTextAlignment align ;

//? ? switch (myAlignment) {

//? ? ? ? case kMyTextAlignmentCenter:{

//? ? ? ? ? ? align = NSTextAlignmentCenter;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case kMyTextAlignmentLeft:{

//? ? ? ? ? ? align = NSTextAlignmentLeft;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case kMyTextAlignmentJustified:{

//? ? ? ? ? ? align= NSTextAlignmentJustified;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case kMyTextAlignmentNatural:{

//? ? ? ? ? ? align = NSTextAlignmentNatural;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case kMyTextAlignmentRight:

//? ? ? ? {

//? ? ? ? ? ? align = NSTextAlignmentRight;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? default:{

//? ? ? ? ? ? align = NSTextAlignmentLeft;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? }

//? ? return align;

//}

//

////將NSTextAlignment 換成 自定義MyCustomStrAlignment

//+(MyCustomStrAlignment)changeTextAlignment:(NSTextAlignment)myAlignment

//{

//? ? MyCustomStrAlignment align ;

//? ? switch (myAlignment) {

//? ? ? ? case NSTextAlignmentCenter :{

//? ? ? ? ? ? align = kMyTextAlignmentCenter;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case NSTextAlignmentLeft :{

//? ? ? ? ? ? align = kMyTextAlignmentLeft;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case NSTextAlignmentJustified :{

//? ? ? ? ? ? align= kMyTextAlignmentJustified;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case NSTextAlignmentNatural :{

//? ? ? ? ? ? align = kMyTextAlignmentNatural;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? case NSTextAlignmentRight :

//? ? ? ? {

//? ? ? ? ? ? align = kMyTextAlignmentRight;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? ? ? default:{

//? ? ? ? ? ? align = kMyTextAlignmentLeft;

//? ? ? ? }

//? ? ? ? ? ? break;

//? ? }

//? ? return align;

//}

#pragma mark? 將異常字符串處理成“”

//處理成空字符串

- (NSString*)dealEmptyMessageString:(NSString*)str

{

? ? str = [NSString stringWithFormat:@"%@",str];

? ? if ([self isMessageNullOrEmpty:str]) {

? ? ? ? return@"";

? ? }else{

? ? ? ? returnstr;

? ? }

}

//字符串是否為空

- (BOOL)isMessageNullOrEmpty:(NSString*)string

{

? ? if ([string isKindOfClass:[NSNull class]]) {

? ? ? ? returnYES;

? ? }

? ? string = [NSStringstringWithFormat:@"%@",string];

? ? returnstring ==nil|| string==(id)[NSNullnull] || [stringisEqualToString:@""] || string.length==0|| [stringisEqualToString:@"(null)"] || [stringisEqualToString:@"null"] || [stringisEqualToString:@"<null>"]|| [stringisEqualToString:@"0(NSNull)"];

}

@end



.h文件

#import <UIKit/UIKit.h>

//typedef enum {

//? ? kMyTextAlignmentLeft? ? ? = 0,

//? ? kMyTextAlignmentCenter? ? = 1,

//? ? kMyTextAlignmentRight? ? = 2,

//? ? kMyTextAlignmentJustified = 3,

//? ? kMyTextAlignmentNatural? = 4

//}MyCustomStrAlignment;

typedefvoid(^KZTCustomAttributeLabelBlock)(void);

@interfaceSTCustomTextView :UITextView

/**(不建議使用) 同步加載帶文本標(biāo)簽字符串 比較卡环凿,解決富文本無(wú)法準(zhǔn)確計(jì)算富文本視圖高度問(wèn)題

?*attributStr :標(biāo)簽文本,可設(shè)置文本居中方式放吩,自適應(yīng)文本視圖大小

?* 缺點(diǎn):*同步加載智听,比較卡,

?* 服務(wù)端配字體大小標(biāo)簽不可使用,字體大小只能客戶端寫(xiě)到推,不然顯示有問(wèn)題

?* 建議:不建議再使用這個(gè)屬性考赛, 建議使用asyncAttributStr屬性,異步)

?**/

@property (nonatomic,copy)NSString * attributStr;

/**(建議使用)異步處理帶文本標(biāo)簽* 解決富文本無(wú)法準(zhǔn)確計(jì)算富文本視圖高度問(wèn)題

?**優(yōu)點(diǎn): 異步加載標(biāo)簽文字莉测,界面比較流暢

?** 缺點(diǎn):同一個(gè)視圖多次賦值颜骤, 會(huì)出現(xiàn)文字重疊字體(顏色加重)的情況,(比如cell重用)

?**服務(wù)端配字體大小標(biāo)簽不可使用,字體大小只能客戶端寫(xiě)捣卤,不然顯示有問(wèn)題

?** 賦值后忍抽,由于是異步不能直接使用該視圖的frame布局,可在attributeBlock屬性的block回調(diào)中處理

?**建議:僅賦值一次或不會(huì)快速刷新多次的地方使用該屬性董朝,)鸠项,強(qiáng)烈建議使用異步,

?*/

@property(nonatomic,copy)NSString* asyncAttributStr;

/**

?字體大小,

?*/

@property (nonatomic, strong) UIFont? *myFont;//

/**

?對(duì)齊方式默認(rèn)居左 kMyTextAlignmentLeft

?*/

//@property (nonatomic, assign) MyCustomStrAlignment myAlignment;//

/**

?設(shè)置字體顏色子姜,如html標(biāo)簽中沒(méi)有

?返回的不是html語(yǔ)言的普通字符串設(shè)置顏色用的祟绊,設(shè)置過(guò)后原來(lái)的顏色就沒(méi)有用了

?*/

@property (nonatomic, strong) UIColor *myColor;

// default is NSLineBreakByTruncatingTail. used for single and multiple lines of text

@property(nonatomic)? ? ? ? NSLineBreakMode? ? myLineBreakMode;

/**行間距**/

@property (nonatomic,assign) CGFloat lineRowSpacing;

/**段后間距**/

@property(nonatomic,assign)CGFloatlineAfterParagraphSpacing;

/**段前間距**/

@property(nonatomic,assign)CGFloatlineBeforeParagraphSpacing;

/**NSAttributedString*

?* myAttributedText

?*/

@property(nonatomic,copy)NSAttributedString* myAttributedText;

/**

?isAdaptiveWidthByHeight

?isAdaptiveHeightByWidth

?不同時(shí)YES,其中一個(gè)為YES時(shí)哥捕,另一個(gè)會(huì)被改為NO牧抽,

?**/

/**固定高度適配寬度 默認(rèn) NO**/

@property (nonatomic,assign) BOOL isAdjustWidthByHeight;

/**固定寬度適配高度 默認(rèn) NO**/

@property (nonatomic,assign) BOOL isAdjustHeightByWidth;

/**異步展示加載asyncAttributStr后調(diào)用block*

?*注 目前僅在asyncAttributStr屬性后調(diào)用

?*注 使用時(shí)要預(yù)防block的循環(huán)引用,必要時(shí)要手動(dòng)打破循環(huán)引用

?*/

@property (nonatomic,copy)KZTCustomAttributeLabelBlock attributeBlock;

/**

?使用后是否將 KZTCustomAttributeLabelBlock 置nil,(手動(dòng)打破循環(huán)),使用時(shí)需先事先設(shè)置

?對(duì)asyncAttributStr屬性有效,

?默認(rèn) NO,block不置nil

?**/

@property(nonatomic,assign)BOOLisDestroyAttributeBlockAfterUse;

//將自定義MyCustomStrAlignment 換成 NSTextAlignment

//+(NSTextAlignment)changeCustomAlignment:(MyCustomStrAlignment)myAlignment;

////將NSTextAlignment 換成 自定義MyCustomStrAlignment

//+(MyCustomStrAlignment)changeTextAlignment:(NSTextAlignment)myAlignment;

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市遥赚,隨后出現(xiàn)的幾起案子扬舒,更是在濱河造成了極大的恐慌,老刑警劉巖鸽捻,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件呼巴,死亡現(xiàn)場(chǎng)離奇詭異泽腮,居然都是意外死亡御蒲,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)诊赊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)厚满,“玉大人,你說(shuō)我怎么就攤上這事碧磅〉夤浚” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵鲸郊,是天一觀的道長(zhǎng)丰榴。 經(jīng)常有香客問(wèn)我,道長(zhǎng)秆撮,這世上最難降的妖魔是什么四濒? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上盗蟆,老公的妹妹穿的比我還像新娘戈二。我一直安慰自己,他們只是感情好喳资,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布觉吭。 她就那樣靜靜地躺著,像睡著了一般仆邓。 火紅的嫁衣襯著肌膚如雪鲜滩。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,604評(píng)論 1 305
  • 那天节值,我揣著相機(jī)與錄音绒北,去河邊找鬼。 笑死察署,一個(gè)胖子當(dāng)著我的面吹牛闷游,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播贴汪,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼脐往,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了扳埂?” 一聲冷哼從身側(cè)響起业簿,我...
    開(kāi)封第一講書(shū)人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎阳懂,沒(méi)想到半個(gè)月后梅尤,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡岩调,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年巷燥,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片号枕。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡缰揪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤纸兔,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布舷暮,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一毫目、第九天 我趴在偏房一處隱蔽的房頂上張望喷斋。 院中可真熱鬧,春花似錦蒜茴、人聲如沸星爪。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)顽腾。三九已至,卻和暖如春诺核,著一層夾襖步出監(jiān)牢的瞬間抄肖,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工窖杀, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留漓摩,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓入客,卻偏偏與公主長(zhǎng)得像管毙,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子桌硫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容