- 1、如下示例舷礼,設(shè)置行距為5的利用富文本使用UITextView進(jìn)行操作
這里沒有優(yōu)化鹃彻,需要優(yōu)化的自行處理
/// 默認(rèn)收起的配置
- (void)configDidDownClose{
NSString * contentsub = @"比特幣(Bitcoin)的概念最初由中本聰在2008年11月1日提出, [50]并于2009年1月3日正式誕生 [1] 妻献。根據(jù)中本聰?shù)乃悸吩O(shè)計(jì)發(fā)布的開源軟件以及建構(gòu)其上的P2P網(wǎng)絡(luò)蛛株。比特幣是一種P2P形式的數(shù)字貨幣 [52] 团赁。比特幣的交易記錄公開透明 [50] 。點(diǎn)對(duì)點(diǎn)的傳輸意味著一個(gè)去中心化的支付系統(tǒng)點(diǎn)對(duì)點(diǎn)的傳輸意味";
self.noteTV.text = [NSString stringWithFormat:@"%@%@",contentsub,@"收起".ntes_localized] ;
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.lineSpacing = 5;
paraStyle.hyphenationFactor = 0.0;
paraStyle.firstLineHeadIndent = 0.0;
paraStyle.paragraphSpacingBefore = 0.0;
paraStyle.headIndent = 0;
paraStyle.tailIndent = 0;
NSDictionary *attributes = @{NSFontAttributeName:FONT(14), NSParagraphStyleAttributeName:paraStyle
};
NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc]init];
NSDictionary *attDict1 = @{NSForegroundColorAttributeName:kColor_ox(0x333333)};
NSAttributedString *attStr1 = [[NSAttributedString alloc] initWithString:[self.noteTV.text substringWithRange:NSMakeRange(0,self.noteTV.text.length - @"收起".ntes_localized.length )] attributes:attDict1];
NSDictionary *attDict2 = @{NSForegroundColorAttributeName:kZhuTiColor};
NSAttributedString *attStr2 = [[NSAttributedString alloc] initWithString:[self.noteTV.text substringWithRange:NSMakeRange(self.noteTV.text.length - @"收起".ntes_localized.length , @"收起".ntes_localized.length)] attributes:attDict2];
[attributedText appendAttributedString:attStr1];
[attributedText appendAttributedString:attStr2];
[attributedText addAttributes:attributes range:NSMakeRange(0, self.noteTV.text.length)];
/// 給富文本后面增加可操作的點(diǎn)擊鏈接通過代理來(lái)實(shí)現(xiàn)
NSString *valueString = [[NSString stringWithFormat:@"didDownClose://%@", @"收起".ntes_localized] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
[attributedText addAttribute:NSLinkAttributeName value:valueString range:NSMakeRange(self.noteTV.text.length - @"收起".ntes_localized.length , @"收起".ntes_localized.length)];
_noteTV.attributedText = attributedText;
CGFloat height = [self.noteTV.text getSpaceLabelWithFont:FONT(14) withWidth:SCREEN_WIDTH - 30];
self.titleContentView = UIView.new;
[self.titleContentView addSubview:self.noteTV];
self.titleContentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height + 15);
self.noteTV.frame = CGRectMake(15, 15, SCREEN_WIDTH - 30, height);
[self.tableView setTableHeaderView:self.titleContentView];
}
/// 默認(rèn)打開的配置
- (void)configDidOpenClose{
NSString * contentsub = @"比特幣(Bitcoin)的概念最初由中本聰在2008年11月1日提出谨履, [50]并于2009年1月3日正式誕生 [1] 欢摄。根據(jù)中本聰?shù)乃悸吩O(shè)計(jì)發(fā)布的開源軟件以及建構(gòu)其上的P2P網(wǎng)絡(luò)。比特幣是一種P2P形式的數(shù)字貨幣 [52] 笋粟。比特幣的交易記錄公開透明 [50] 怀挠。點(diǎn)對(duì)點(diǎn)的傳輸意味著一個(gè)去中心化的支付系統(tǒng)點(diǎn)對(duì)點(diǎn)的傳輸意味";
NSString *tempStr = [self stringByTruncatingString:contentsub suffixStr:@"...全部展開" font:FONT(14) width:SCREEN_WIDTH - 30 num:3];
self.noteTV.text = tempStr;
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.lineSpacing = 5;
paraStyle.hyphenationFactor = 0.0;
paraStyle.firstLineHeadIndent = 0.0;
paraStyle.paragraphSpacingBefore = 0.0;
paraStyle.headIndent = 0;
paraStyle.tailIndent = 0;
NSDictionary *attributes = @{NSFontAttributeName:FONT(14), NSParagraphStyleAttributeName:paraStyle
};
NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc]init];
NSDictionary *attDict1 = @{NSForegroundColorAttributeName:kColor_ox(0x333333)};
NSAttributedString *attStr1 = [[NSAttributedString alloc] initWithString:[tempStr substringWithRange:NSMakeRange(0,tempStr.length - @"全部展開".ntes_localized.length )] attributes:attDict1];
NSDictionary *attDict2 = @{NSForegroundColorAttributeName:kZhuTiColor};
NSAttributedString *attStr2 = [[NSAttributedString alloc] initWithString:[tempStr substringWithRange:NSMakeRange(tempStr.length - @"全部展開".ntes_localized.length , @"全部展開".ntes_localized.length)] attributes:attDict2];
[attributedText appendAttributedString:attStr1];
[attributedText appendAttributedString:attStr2];
[attributedText addAttributes:attributes range:NSMakeRange(0, tempStr.length)];
/// 給富文本后面增加可操作的點(diǎn)擊鏈接通過代理來(lái)實(shí)現(xiàn)
NSString *valueString = [[NSString stringWithFormat:@"didOpenClose://%@", @"全部展開".ntes_localized] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
[attributedText addAttribute:NSLinkAttributeName value:valueString range:NSMakeRange(tempStr.length - @"全部展開".ntes_localized.length , @"全部展開".ntes_localized.length)];
_noteTV.attributedText = attributedText;
CGFloat height = [tempStr getSpaceLabelWithFont:FONT(14) withWidth:SCREEN_WIDTH - 30];
self.titleContentView = UIView.new;
[self.titleContentView addSubview:self.noteTV];
self.titleContentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height + 15);
self.noteTV.frame = CGRectMake(15, 15, SCREEN_WIDTH - 30, height);
[self.tableView setTableHeaderView:self.titleContentView];
}
/// 將文本按長(zhǎng)度度截取并加上指定后綴
/// @param str 文本
/// @param suffixStr 指定后綴
/// @param font 文本字體
/// @param textWidth 文本長(zhǎng)度
/// @param num 多少行
- (NSString*)stringByTruncatingString:(NSString *)str suffixStr:(NSString *)suffixStr font:(UIFont *)font width:(CGFloat)textWidth num:(NSInteger)num {
if (!str) return nil;
CGFloat width = 0;
int k = 0;
CGFloat suffixWidth = [suffixStr sizeWithAttributes:@{NSFontAttributeName:font}].width;
if (str && [str isKindOfClass:[NSString class]]) {
for (int i=0; i< [str length];i++){
NSString *tempStr = [str substringToIndex:i + 1];
CGSize size = [tempStr sizeWithAttributes:@{NSFontAttributeName:font}];
if ((size.width - width) > textWidth) {
tempStr = [str substringToIndex:i];
if (k == num - 1) {
str = tempStr;
for (int i=(int)[str length] - (int)[suffixStr length]; i< [str length];i = i - (int)[suffixStr length]){
tempStr = [str substringToIndex:i];
CGSize size = [tempStr sizeWithAttributes:@{NSFontAttributeName:font}];
if(size.width - width + suffixWidth < textWidth){
tempStr = [NSString stringWithFormat:@"%@%@", tempStr,suffixStr];
str = tempStr;
break;
}
}
break;
}
width = size.width ;
k ++;
}
}
}
return str;
}
- 3害捕、通過UITextView的代理方法來(lái)實(shí)現(xiàn)
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
if ([[URL scheme] isEqualToString:@"didOpenClose"]) {
[self configDidOpenClose];
return NO;
}
if ([[URL scheme] isEqualToString:@"didDownClose"]) {
[self configDidDownClose];
return NO;
}
return YES;
}
- (CGFloat)getSpaceLabelWithFont:(UIFont*)font withWidth:(CGFloat)width {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.lineSpacing = 5;
paraStyle.hyphenationFactor = 0.0;
paraStyle.firstLineHeadIndent = 0.0;
paraStyle.paragraphSpacingBefore = 0.0;
paraStyle.headIndent = 0;
paraStyle.tailIndent = 0;
NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
};
CGSize size = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
return size.height;
}