富文本NSAttributeString
Posted Da雪山
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了富文本NSAttributeString相关的知识,希望对你有一定的参考价值。
例子:http://www.jianshu.com/p/9ffcdc0003e0
下面研究下AttributedString究竟可以设置哪些属性,具体来说,有以下21个:
#pragma mark - 富文本操作
/**1
* 单纯改变一句话中的某些字的颜色
*
* @param color 需要改变成的颜色
* @param totalStr 总的字符串
* @param subArray 需要改变颜色的文字数组
*
* @return 生成的富文本
*/
- (NSMutableAttributedString *)ls_changeCorlorWithColor:(UIColor *)color TotalString:(NSString *)totalStr SubStringArray:(NSArray *)subArray {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
for(NSString *rangeStr in subArray) {
NSRange range = [totalStr rangeOfString:rangeStr options:NSBackwardsSearch];
[attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
}
return attributedStr;
}
/**2
* 单纯改变句子的字间距(需要 <CoreText/CoreText.h>)
*
* @param totalString 需要更改的字符串
* @param space 字间距
*
* @return 生成的富文本
*/
- (NSMutableAttributedString *)ls_changeSpaceWithTotalString:(NSString *)totalString Space:(CGFloat)space {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
long number = space;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[attributedStr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedStr length])];
CFRelease(num);
return attributedStr;
}
/** 3
* 单纯改变段落的行间距
*
* @param totalString 需要更改的字符串
* @param lineSpace 行间距
*
* @return 生成的富文本
*/
- (NSMutableAttributedString *)ls_changeLineSpaceWithTotalString:(NSString *)totalString LineSpace:(CGFloat)lineSpace {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalString length])];
return attributedStr;
}
/**4
* 同时更改行间距和字间距
*
* @param totalString 需要改变的字符串
* @param lineSpace 行间距
* @param textSpace 字间距
*
* @return 生成的富文本
*/
- (NSMutableAttributedString *)ls_changeLineAndTextSpaceWithTotalString:(NSString *)totalString LineSpace:(CGFloat)lineSpace textSpace:(CGFloat)textSpace {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalString length])];
long number = textSpace;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[attributedStr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedStr length])];
CFRelease(num);
return attributedStr;
}
/** 5
* 改变某些文字的颜色 并单独设置其字体
*
* @param font 设置的字体
* @param color 颜色
* @param totalString 总的字符串
* @param subArray 想要变色的字符数组
*
* @return 生成的富文本
*/
- (NSMutableAttributedString *)ls_changeFontAndColor:(UIFont *)font Color:(UIColor *)color TotalString:(NSString *)totalString SubStringArray:(NSArray *)subArray {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
for (NSString *rangeStr in subArray) {
NSRange range = [totalString rangeOfString:rangeStr options:NSBackwardsSearch];
[attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
[attributedStr addAttribute:NSFontAttributeName value:font range:range];
}
return attributedStr;
}
/** 6
* 为某些文字改为链接形式
*
* @param totalString 总的字符串
* @param subArray 需要改变颜色的文字数组(要是有相同的 只取第一个)
*
* @return 生成的富文本
*/
- (NSMutableAttributedString *)ls_addLinkWithTotalString:(NSString *)totalString SubStringArray:(NSArray *)subArray {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
for (NSString *rangeStr in subArray) {
NSRange range = [totalString rangeOfString:rangeStr options:NSBackwardsSearch];
[attributedStr addAttribute:NSLinkAttributeName value:totalString range:range];
}
return attributedStr;
}
#warning //链接(Label中的链接不能点击,UITextView中的链接可以点击)
NSString *total6 = @"同时改行间距和字间距,此时行间距为10>>详情";
NSArray *arr = @[@">>详情"];
self.label6.attributedText = [self ls_addLinkWithTotalString:total6 SubStringArray:arr];
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 100, 200, 200)];
textView.backgroundColor = [UIColor greenColor];
textView.delegate = self;
textView.scrollEnabled = NO;
textView.editable = NO;
textView.font = [UIFont systemFontOfSize:15];
NSString *str = @"今天是Ⅰ类账户 个好日子好日子好日子好日子好日子好日子>>详情";
[self.view addSubview:textView];
NSString *strr = [self replaceString:str OfRange:NSMakeRange(3, 4) WithString:@"Ⅲ类账户"];
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:strr];
NSRange range = [str rangeOfString:@">>详情"];
// [attStr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.google.com"] range:range];
[attStr addAttribute:NSLinkAttributeName value: @" " range:range];
[attStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, 4)];
textView.attributedText = attStr;
#pragma mark UITextView Delegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
NSLog(@"响应=============%@",URL);
// NextViewController *vc = [[NextViewController alloc]init];
// [self presentViewController:vc animated:YES completion:nil];
return YES;
}
以上是关于富文本NSAttributeString的主要内容,如果未能解决你的问题,请参考以下文章