NSMutableAttributedString富文本

富文本的实现(NSMuttableAttstring/NSMutableAttributedString)

设置段落
1.NSMutableParagraphStyle

行间距
@property(NS_NONATOMIC_IOSONLY) CGFloat lineSpacing;
段落与段落之间的间距
@property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacing;
文本对齐
@property(NS_NONATOMIC_IOSONLY) NSTextAlignment alignment;
首行缩进
@property(NS_NONATOMIC_IOSONLY) CGFloat firstLineHeadIndent;
段落左边缩进
@property(NS_NONATOMIC_IOSONLY) CGFloat headIndent;
段落右边缩进
@property(NS_NONATOMIC_IOSONLY) CGFloat tailIndent;
断行方式
@property(NS_NONATOMIC_IOSONLY) NSLineBreakMode lineBreakMode;
最小行高
@property(NS_NONATOMIC_IOSONLY) CGFloat minimumLineHeight;
最大行高
@property(NS_NONATOMIC_IOSONLY) CGFloat maximumLineHeight;
句子方向
@property(NS_NONATOMIC_IOSONLY) NSWritingDirection baseWritingDirection;
段首空间
@property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacingBefore;

使用方法:
NSMutableAttributedString 设置段落的属性
NSFontAttributeName 字体大小
NSParagraphStyleAttributeName 段落样式
NSDictionary *dict = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:pStyle};
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string attributes:dict];

2.设置指定文字的各种属性
[string addAttribute:对应的属性
value:[UIColor redColor]
range:NSMakeRange(2, 2)];

addAttribute属性值:
NSFontAttributeName 文字大小

NSForegroundColorAttributeName 文字颜色

NSBackgroundColorAttributeName 文字所在区域的背景颜色

NSLigatureAttributeName 0为不连笔;1为默认连笔,也是默认值;

NSKernAttributeName 字距属性

NSStrikethroughStyleAttributeName 删除线 默认没有删除线
NSUnderlineStyle枚举值

NSLinkAttributeName 增加链接文字 要使用textView label没有效果
[string addAttribute:NSLinkAttributeName
value:[NSURL URLWithString:@”http://www.baidu.com“]
range:NSMakeRange(2, 20)];

textView.editable = NO; 编辑框不能处于编辑状态

返回值表示是否使用系统浏览器打开链接
- (BOOL)textView:(UITextView )textView shouldInteractWithURL:(NSURL )URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0)
{
NSLog(@”%@”,URL);
return NO;
}

NSShadowAttributeName 设置文字阴影

NSObliquenessAttributeName 设置倾斜度

插入图片
NSString *imageName = [NSString stringWithFormat:@”%d”, arc4random()%4];
//创建附件
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
textAttachment.image = [UIImage imageNamed:imageName];; //设置图片
textAttachment.bounds = CGRectMake(0, -5, 25, 25); //图片范围

//创建不可变富文本(包含图片附件)
NSAttributedString *textString = [NSAttributedString attributedStringWithAttachment:textAttachment];

[string insertAttributedString:textString atIndex:40];

Demo

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //NSAttributedString
    //NSMutableAttributedString
    //NSFontAttributeName 文字的大小

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@”哈斯卡减肥哈客户” attributes:nil];

    [attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24.0f] range:NSMakeRange(0, 4)];

    //[attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4, 4)];

    [attrString addAttribute:NSKernAttributeName value:@1 range:NSMakeRange(0, attrString.length)];

    [attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, attrString.length)];

    [attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, attrString.length)];

    //[attrString addAttribute:NSObliquenessAttributeName value:@1 range:NSMakeRange(0, attrString.length)];

    NSShadow *shadow = [[NSShadow alloc] init];

    shadow.shadowColor = [UIColor orangeColor];
    shadow.shadowOffset = CGSizeMake(-5, 5);

    //shadow.shadowBlurRadius = 10;

    [attrString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, attrString.length)];

    //插入图片
    NSTextAttachment *textImage = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textImage.image = [UIImage imageNamed:@”1”];
    textImage.bounds = CGRectMake(0, 0, 20, 20);

    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:textImage];
    [attrString insertAttributedString:string atIndex:4];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 375, 40)];
    label.attributedText = attrString;

    [self.view addSubview:label];

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 100, 375, 100)];

    NSMutableAttributedString *linkString = [[NSMutableAttributedString alloc] initWithString:@”www.baidu.com”];

    [linkString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@”http://www.baidu.com“] range:NSMakeRange(0, linkString.length)];

    textView.attributedText = linkString;

    textView.editable = NO;

    textView.delegate = self;

    [self.view addSubview:textView];

}

//是否用浏览器打开该链接 YES
- (BOOL)textView:(UITextView )textView shouldInteractWithURL:(NSURL )URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0)
{
NSLog(@”%@”,URL);
return YES;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值