NSAttributeString和NSMutableAttributeString

本文介绍如何使用Objective-C创建富文本,并详细解释了如何为文本添加粗体、高亮、下划线、字体颜色以及URL链接等功能。此外,还展示了如何在文本中插入图片附件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/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;


效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值