utableAttributedString在我的UITextV

utableAttributedString在我的UITextV

本文介绍了如何使用NSMutableAttributedString在我的UITextView中加粗一些单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITextView,并且我正在使用NSString stringWithFormat投射某些单词,我想加粗。

I have a UITextView and there are certain words I'm casting with NSString stringWithFormat that I'd like to be bolded.

我查看了Stack Overflow和试图按照帖子,但我想我不理解它。

I have looked around Stack Overflow and tried to follow the the postings but I guess I'm not understanding it.

这是我一直在玩的东西:

Here's what I've been playing around with:

    NSRange boldedRange = NSMakeRange(0, 4);
    NSString *boldFontName = [[UIFont fontWithName:@"Helvetica-Bold" size:100]fontName];


    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.name];

    [attrString beginEditing];
    [attrString addAttribute:NSFontAttributeName
                       value:boldFontName
                       range:boldedRange];
    [attrString endEditing];

    self.resultsTextView.attributedText = attrString;


    self.resultsTextView.text = [NSString stringWithFormat:@"One day, %@ was taking a walk and saw a %@ boy.  He was %@ a %@.", attrString, self.adjective, self.adverb, self.noun];


推荐答案

如果你想要你也可以按照以下方式设置通过设置字典作为整体,作为属性

You can also set it the following way if you want by setting a dictionary as a whole, as attribute

NSString *strTextView = @"This is some demo Text to set BOLD";

NSRange rangeBold = [strTextView rangeOfString:@"BOLD"];

UIFont *fontText = [UIFont boldSystemFontOfSize:10];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];

NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:strTextView];
[mutAttrTextViewString setAttributes:dictBoldText range:rangeBold];

[textViewTermsPolicy setAttributedText:mutAttrTextViewString];

这篇关于如何使用NSMutableAttributedString在我的UITextView中加粗一些单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:54