富文本NSAttributeString

例子:http://www.jianshu.com/p/9ffcdc0003e0

下面研究下AttributedString究竟可以设置哪些属性,具体来说,有以下21个:

 
// NSFontAttributeName                设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
// NSForegroundColorAttributeNam      设置字体颜色,取值为 UIColor对象,默认值为黑色
// NSBackgroundColorAttributeName     设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
// NSLigatureAttributeName            设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
// NSKernAttributeName                设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
// NSStrikethroughStyleAttributeName  设置删除线,取值为 NSNumber 对象(整数)
// NSStrikethroughColorAttributeName  设置删除线颜色,取值为 UIColor 对象,默认值为黑色
// NSUnderlineStyleAttributeName      设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
// NSUnderlineColorAttributeName      设置下划线颜色,取值为 UIColor 对象,默认值为黑色
// NSStrokeWidthAttributeName         设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
// NSStrokeColorAttributeName         填充部分颜色,不是字体颜色,取值为 UIColor 对象
// NSShadowAttributeName              设置阴影属性,取值为 NSShadow 对象
// NSTextEffectAttributeName          设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
// NSBaselineOffsetAttributeName      设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
// NSObliquenessAttributeName         设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
// NSExpansionAttributeName           设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
// NSWritingDirectionAttributeName    设置文字书写方向,从左向右书写或者从右向左书写
// NSVerticalGlyphFormAttributeName   设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
// NSLinkAttributeName                设置链接属性,点击后调用浏览器打开指定URL地址
// NSAttachmentAttributeName          设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
// NSParagraphStyleAttributeName      设置文本段落排版格式,取值为 NSParagraphStyle 对象
 
 

#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;

}

 
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/daxueshan/p/5814445.html