Ios 如何使用NSMutableAttributedString在我的UITextView中加粗一些单词?
我有一个UITextView,我正在使用NSString stringWithFormat转换某些单词,我希望用粗体显示 我环顾了一下堆栈溢出,并试图跟踪帖子的内容,但我想我不明白 以下是我一直在玩的东西:Ios 如何使用NSMutableAttributedString在我的UITextView中加粗一些单词?,ios,objective-c,uitextview,nsattributedstring,nsmutableattributedstring,Ios,Objective C,Uitextview,Nsattributedstring,Nsmutableattributedstring,我有一个UITextView,我正在使用NSString stringWithFormat转换某些单词,我希望用粗体显示 我环顾了一下堆栈溢出,并试图跟踪帖子的内容,但我想我不明白 以下是我一直在玩的东西: NSRange boldedRange = NSMakeRange(0, 4); NSString *boldFontName = [[UIFont fontWithName:@"Helvetica-Bold" size:100]fontName]; NSMuta
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];
输出:
查看@Bbrame和@BenoitJadinon上的@CrazyyyOghart改进,了解前一个SO问题
我已经用了一年多了,效果很好。一个限制:如果同一个字符串在原始字符串中出现多次,我认为不能将其加粗多次。但是,如果您愿意,您可以扩展代码使其这样做。如果您还需要从UITextView中过滤一些单词,并使其加下划线/更改特定文本的颜色,那么您可以使用以下代码 在这里,我将获取内容字符串中的所有文档文本,并过滤希伯来语中的一些特定文本
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];
}
}
//用于获取点击手势中的文本位置
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];
}
}
我已经有一段时间没有使用属性字符串了,但在我看来,您的问题是您正在将
resultsTextView
设置为属性字符串,但下一行您将其设置为非属性NSString
NSString
没有属性,因此您基本上是在“撤消”您“做过”的任何属性。如果我没记错的话,你不能同时拥有attributedText
和text
属性,这两个属性都是。请参见答案,你可以在文本视图中将特定文本加粗。@KiritModi我知道你可以配对。但请参见问题,更具体地说是最后一行代码。他最终将NSString
用于他的UITextView
,而不是NSAttributedString
。您不能在标准NSString
中加粗文本。无法根据最后一行将NSStting设置为加粗文本。您只能将nsattributed string与加粗文本和常规文本混合使用。如果我想加粗除加粗外的另一个词,该怎么办?例如,这是一些要设置为粗体的演示文本?我希望您可能已经解决了您面临的问题,但如果您没有将“粗体”替换为“演示文本”,我已经使用了这段代码,这对我很有用。谢谢