如何使用 NSMutableAttributedString 在我的 UITextView 中加粗一些单词?
How to bold some words in my UITextView using NSMutableAttributedString?
我有一个 UITextView 并且我使用 NSString stringWithFormat 投射了某些我想要加粗的词。
我查看了 Stack Overflow 并尝试关注帖子,但我想我不理解它。
这是我一直在玩的东西:
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];
你也可以按照下面的方式设置,把字典作为一个整体设置为属性
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];
使用以下代码在 TextView 中设置属性字符串。
NSString *infoString =@"I am Kirit Modi from Deesa.";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
UIFont *font_regular=[UIFont fontWithName:@"Helvetica" size:20.0f];
UIFont *font_bold=[UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
[attString addAttribute:NSFontAttributeName value:font_regular range:NSMakeRange(0, 4)];
[attString addAttribute:NSFontAttributeName value:font_bold range:NSMakeRange(5, 15)];
[attString addAttribute:NSFontAttributeName value:font_regular range:NSMakeRange(16, infoString.length - 15 - 1)];
[self.txtView setAttributedText:attString];
输出:
查看@CrazyYoghurt 对@Bbrame 和@BenoitJadinon 对上一个 SO 问题的改进 3586871
我已经使用它一年多了,效果很好。一个限制:如果同一个字符串在您的原始字符串中出现多次,我认为您不能将其加粗多次。但是如果您愿意,您可以扩展代码来实现它。
如果您还需要从 UITextView 中过滤一些单词并使其成为特定文本的 underline/change 颜色,那么您可以使用以下代码。
在这里,我获取了 Content 字符串中的所有文档文本,并过滤了一些希伯来语的特定文本。
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:content attributes:nil];
[aStr addAttribute:NSLinkAttributeName value:@"http://www.apple.com" range:[content rangeOfString:@"מדיניות פרטיות"]];
[aStr addAttribute:NSLinkAttributeName value:@"http://www.google.com" range:[content rangeOfString:@"לינק"]];
textview.linkTextAttributes = @{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
textview.delegate = (id)self;
//...您可以根据您的自定义颜色
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[content rangeOfString:@"מדיניות פרטיות"]];
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[content rangeOfString:@"לינק"]];
//在这里您还可以在该文本上添加点击手势。
//点击手势
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedTextView:)];
[textview addGestureRecognizer:tapRecognizer];
[textview setAttributedText:aStr];
textview.textAlignment=NSTextAlignmentRight;
//用于获取点击手势中的文本位置
-(void)tappedTextView:(UITapGestureRecognizer *)tapGesture
{
UITextView *textView = (UITextView *)tapGesture.view;
CGPoint tapLocation = [tapGesture locationInView:textView];
UITextPosition *textPosition = [textView closestPositionToPoint:tapLocation];
NSDictionary *attributes = [textView textStylingAtPosition:textPosition inDirection:UITextStorageDirectionForward];
NSString *urlStr = attributes[NSLinkAttributeName];
if (urlStr)
{
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",url]]];
PrivacyViewController *next = [PrivacyViewController new];
[self.navigationController pushViewController:next animated:YES];
}
}
我有一个 UITextView 并且我使用 NSString stringWithFormat 投射了某些我想要加粗的词。
我查看了 Stack Overflow 并尝试关注帖子,但我想我不理解它。
这是我一直在玩的东西:
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];
你也可以按照下面的方式设置,把字典作为一个整体设置为属性
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];
使用以下代码在 TextView 中设置属性字符串。
NSString *infoString =@"I am Kirit Modi from Deesa.";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
UIFont *font_regular=[UIFont fontWithName:@"Helvetica" size:20.0f];
UIFont *font_bold=[UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
[attString addAttribute:NSFontAttributeName value:font_regular range:NSMakeRange(0, 4)];
[attString addAttribute:NSFontAttributeName value:font_bold range:NSMakeRange(5, 15)];
[attString addAttribute:NSFontAttributeName value:font_regular range:NSMakeRange(16, infoString.length - 15 - 1)];
[self.txtView setAttributedText:attString];
输出:
查看@CrazyYoghurt 对@Bbrame 和@BenoitJadinon 对上一个 SO 问题的改进 3586871 我已经使用它一年多了,效果很好。一个限制:如果同一个字符串在您的原始字符串中出现多次,我认为您不能将其加粗多次。但是如果您愿意,您可以扩展代码来实现它。
如果您还需要从 UITextView 中过滤一些单词并使其成为特定文本的 underline/change 颜色,那么您可以使用以下代码。
在这里,我获取了 Content 字符串中的所有文档文本,并过滤了一些希伯来语的特定文本。
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:content attributes:nil];
[aStr addAttribute:NSLinkAttributeName value:@"http://www.apple.com" range:[content rangeOfString:@"מדיניות פרטיות"]];
[aStr addAttribute:NSLinkAttributeName value:@"http://www.google.com" range:[content rangeOfString:@"לינק"]];
textview.linkTextAttributes = @{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
textview.delegate = (id)self;
//...您可以根据您的自定义颜色
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[content rangeOfString:@"מדיניות פרטיות"]];
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[content rangeOfString:@"לינק"]];
//在这里您还可以在该文本上添加点击手势。 //点击手势
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedTextView:)];
[textview addGestureRecognizer:tapRecognizer];
[textview setAttributedText:aStr];
textview.textAlignment=NSTextAlignmentRight;
//用于获取点击手势中的文本位置
-(void)tappedTextView:(UITapGestureRecognizer *)tapGesture
{
UITextView *textView = (UITextView *)tapGesture.view;
CGPoint tapLocation = [tapGesture locationInView:textView];
UITextPosition *textPosition = [textView closestPositionToPoint:tapLocation];
NSDictionary *attributes = [textView textStylingAtPosition:textPosition inDirection:UITextStorageDirectionForward];
NSString *urlStr = attributes[NSLinkAttributeName];
if (urlStr)
{
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",url]]];
PrivacyViewController *next = [PrivacyViewController new];
[self.navigationController pushViewController:next animated:YES];
}
}