NSMutableAttributedString && NSAttributedString

本文详细探讨了NSMutableAttributedString和NSAttributedString在iOS开发中的使用,包括属性设置、链接处理等核心概念,帮助开发者更好地掌握文本处理技巧。
摘要由CSDN通过智能技术生成

UILabel *label = [[UILabel alloc] init];
    
    
    label.frame = CGRectMake(30, 60 , [UIScreen mainScreen].bounds.size.width - 60, 340);
    label.numberOfLines = 0;

    [self.view addSubview:label];
    
    
    NSString *string = @"“萝卜白菜”是一家与移动医疗项目为主的创业型公司,公司具有8年医药行业杂志经营经验,积累众多医药界人脉和政府资源。创业团队由具有深厚医药学背景和互联网成功经验的团队组成,项目还在研发中,就已筹集到2千万人民币的投资。www.baidu.com";
    
    NSMutableAttributedString *attStirng = [[NSMutableAttributedString alloc] initWithString:string];
    
    
    [attStirng addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:21] range:NSMakeRange(0, attStirng.length)];
    
    
    设置字体的大小    NSFontAttributeName;
    [attStirng addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:30] range:NSMakeRange(0, 6)];
    
    
    //设置文字颜色 NSForegroundColorAttributeName;
    [attStirng addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 6)];
    
    
    //设置文字的背景颜色 NSBackgroundColorAttributeName;
    [attStirng addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 6)];
    
    
    //设置文字间的距离。0分开 负数重叠  NSKernAttributeName;
    [attStirng addAttribute:NSKernAttributeName value:@5 range:NSMakeRange(0, 6)];
    
    
    //设置文字删除线样式  NSStrikethroughStyleAttributeName;
    [attStirng addAttribute:NSStrikethroughStyleAttributeName value:@4 range:NSMakeRange(7, 10)];
    
    //设置文字删除线颜色    NSStrikethroughColorAttributeName;
    [attStirng addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:NSMakeRange(7, 10)];
    
    //设置文字下划线样式    NSUnderlineStyleAttributeName;
    [attStirng addAttribute:NSUnderlineStyleAttributeName value:@2 range:NSMakeRange(17, 10)];
    
    
    //文字下划线颜色    NSUnderlineColorAttributeName;
    [attStirng addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:NSMakeRange(17, 10)];
    
    
    //文字笔画颜色  NSStrokeColorAttributeName;
    [attStirng addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(27, 20)];
    
    //设置文字笔画宽度的百分比     NSStrokeWidthAttributeName;
    [attStirng addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:2.0] range:NSMakeRange(27, 10)];
    
    
    [attStirng addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-2.0] range:NSMakeRange(37, 10)];
    
    
    
    //设置文字的阴影属相    NSShadowAttributeName;
    NSShadow *sha = [[NSShadow alloc] init];
    sha.shadowOffset = CGSizeMake(0, 5);
    
    [attStirng addAttribute:NSShadowAttributeName value:sha range:NSMakeRange(47, 10)];
    
  
    //设置文本效果    NSTextEffectAttributeName;
    
    //NSTextEffectLetterpressStyle 没有效果??
    [attStirng addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:NSMakeRange(57, 8)];
    
    
    //设置文字附件属性(图文混排相关)    NSAttachmentAttributeName;
    NSTextAttachment *catMent = [[NSTextAttachment alloc] init];
    catMent.image = [UIImage imageNamed:@"cat.jpg"];
    catMent.bounds = CGRectMake(0, 0, 50, 50);
    
    NSAttributedString *insertStr = [NSAttributedString attributedStringWithAttachment:catMent];
    [attStirng insertAttributedString:insertStr atIndex:65];
    

    
    //设置文字基线偏移量    NSBaselineOffsetAttributeName;
    [attStirng addAttribute:NSBaselineOffsetAttributeName value:@4 range:NSMakeRange(70, 4)];
    
    [attStirng addAttribute:NSBaselineOffsetAttributeName value:@(-4) range:NSMakeRange(80, 4)];

    //设置文字斜体效果    NSObliquenessAttributeName;
    [attStirng addAttribute:NSObliquenessAttributeName value:@1 range:NSMakeRange(85, 5)];
   
    
    //设置文字向左右拉伸    NSExpansionAttributeName;
    [attStirng addAttribute:NSExpansionAttributeName value:@1.0 range:NSMakeRange(90, 5)];
    
    //设置文字的书写样式    NSWritingDirectionAttributeName;
    //只能是 0 1 2 3
    //3 是倒叙。  0 1 2 没发现什么东西
    [attStirng addAttribute:NSWritingDirectionAttributeName value:@[@3] range:NSMakeRange(95, 5)];
    
    
    label.attributedText = attStirng;

NSLinkAttributeName

对于这个文字的网络链接效果,在UILabel是没有效果的。而且,对于UITextView的非编辑状态才好使。


    UITextView *bottm = [[UITextView alloc] init];
    bottm.frame = CGRectMake(30, 400, 200, 100);
    bottm.backgroundColor = [UIColor redColor];
    bottm.delegate = self;
    bottm.editable = NO;
    [self.view addSubview:bottm];
    
    
    NSMutableAttributedString *bottomS = [[NSMutableAttributedString alloc] init];
    
    NSURL *url = [NSURL URLWithString:@"www.baidu.com"];

    //设置文字的网络链接效果    NSLinkAttributeName;
    NSAttributedString *baiduLink = [[NSAttributedString alloc] initWithString:@"百度一下" attributes:@{NSLinkAttributeName:url,NSFontAttributeName:[UIFont systemFontOfSize:21]}];

    [bottomS insertAttributedString:baiduLink atIndex:0];
    
    bottm.attributedText = bottomS;

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);
{
    
    //    [ [ UIApplication sharedApplication] openURL:URL];
    
    return YES;
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值