首页 > 其他 > 详细

NSAttributeString和NSMutableAttributeString

时间:2015-03-27 01:18:10      阅读:375      评论:0      收藏:0      [点我收藏+]
UIFontDescriptor *bodyFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
    self.textView.font = [UIFont fontWithDescriptor:bodyFontDescriptor size:0];

    self.textView.textColor = [UIColor blackColor];
    self.textView.backgroundColor = [UIColor whiteColor];
    self.textView.scrollEnabled = YES;

    // Let's modify some of the attributes of the attributed string.
    // You can modify these attributes yourself to get a better feel for what they do.
    // Note that the initial text is visible in the storyboard.
    //全文行距20
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[[NSAttributedString alloc]initWithString:self.textView.text attributes:@{NSBaselineOffsetAttributeName:@(20)}]];

    NSString *text = self.textView.text;

    // Find the range of each element to modify.
    NSRange boldRange = [text rangeOfString:NSLocalizedString(@"bold", nil)];
    NSRange highlightedRange = [text rangeOfString:NSLocalizedString(@"highlighted", nil)];
    NSRange underlinedRange = [text rangeOfString:NSLocalizedString(@"underlined", nil)];
    NSRange tintedRange = [text rangeOfString:NSLocalizedString(@"tinted", nil)];
    NSRange linkRange = [text rangeOfString:NSLocalizedString(@"linkToMyblog", nil)];

    // Add bold. 粗体
    UIFontDescriptor *boldFontDescriptor = [self.textView.font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
    UIFont *boldFont = [UIFont fontWithDescriptor:boldFontDescriptor size:0];
    [attributedText addAttribute:NSFontAttributeName value:boldFont range:boldRange];

    // Add highlight. 高亮
    [attributedText addAttribute:NSBackgroundColorAttributeName value:[UIColor aapl_applicationGreenColor] range:highlightedRange];

    // Add underline. 下划线
    [attributedText addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:underlinedRange];

    // Add tint.字体颜色
    [attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:tintedRange];
    //URL链接
    NSURL *linkUrl = [NSURL URLWithString:@"http://blog.csdn.net/awaylin113"];
    [attributedText addAttribute:NSLinkAttributeName value:linkUrl range:linkRange];
    
    // Add an image attachment.增加附件
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    UIImage *image = [UIImage imageNamed:@"ask"];
    textAttachment.image = image;
    textAttachment.bounds = CGRectMake(0, 0, image.size.width, image.size.height);

    NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [attributedText appendAttributedString:textAttachmentString];
    
    self.textView.attributedText = attributedText;


效果图:

技术分享

NSAttributeString和NSMutableAttributeString

原文:http://blog.csdn.net/awaylin113/article/details/44657335

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!