ios – 在UITextView中突出显示一行

ios – 在UITextView中突出显示一行,第1张

概述我有一个UITextView,它有一个NSAttributedString,每行之间有两个换行符.我试图在textview中突出显示一行.我可以简单地在textview中突出显示一个句子而没有任何问题.但是,我需要从行的开头(textview的左侧)到行尾(textview的右侧)突出显示句子 我使用下面的代码来突出显示来自这个Github Sample的一行.由于每行都有两个换行符,我想在突出显 我有一个UITextVIEw,它有一个NSAttributedString,每行之间有两个换行符.我试图在textvIEw中突出显示一行.我可以简单地在textvIEw中突出显示一个句子而没有任何问题.但是,我需要从行的开头(textvIEw的左侧)到行尾(textvIEw的右侧)突出显示句子

我使用下面的代码来突出显示来自这个Github Sample的一行.由于每行都有两个换行符,我想在突出显示实际句子之后我还可以获得上一个和下一个换行符并突出显示它们.但结果并不像我想要的那样.

// 1. create some FontsUIFontDescriptor* FontDescriptor = [UIFontDescriptor                                    preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];UIFontDescriptor* boldFontDescriptor = [FontDescriptor                                        FontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];UIFont* boldFont =  [UIFont FontWithDescriptor:boldFontDescriptor size: 0.0];UIFont* normalFont =  [UIFont preferredFontForTextStyle:UIFontTextStyleBody];if (!_highlighteDWorld) {    _highlighteDWorld = @"";}// Search for the highlighted world by using regex and highlightNsstring* regexStr = _highlighteDWorld;NSRegularExpression* regex = [NSRegularExpression                              regularExpressionWithPattern:regexStr                              options:0                              error:nil];// Highlighted attributes (Cyan Font color,Bold)NSDictionary* boldAttributes = @{ NSFontAttributename : boldFont,NSBackgroundcolorAttributename    :   [UIcolor cyancolor]};//normal attributesNSDictionary* normalAttributes = @{ NSFontAttributename : normalFont,NSBackgroundcolorAttributename    :   [UIcolor whitecolor] };// If the textvIEw has multiple matches,iterate over each match,and highlight each.[regex enumerateMatchesInString:[_backingStore string]                        options:0                          range:searchRange                     usingBlock:^(NSTextCheckingResult *match,NSMatchingFlags flags,BOol *stop){                         // Highlight the actual line                         NSRange matchRange = [match rangeAtIndex:0];                         [self addAttributes:boldAttributes range:matchRange];                         // Get the newline after the highlighted sentence,and highlight it                         // Check if we reached the end                         if (matchRange.location+matchRange.length < [_backingStore string].length) {                             [self addAttributes:boldAttributes range:NSMakeRange(matchRange.location+matchRange.length,1)];                         }                         // Get the newline before the highlighted sentence,and highlight it                         // Check if we reached the beginning                         if (matchRange.location != 0) {                             [self addAttributes:boldAttributes range:NSMakeRange(matchRange.location-1,1)];                         }                         // 4. reset the style to the original                         if (NSMaxrange(matchRange)+1 < self.length) {                             [self addAttributes:normalAttributes                                           range:NSMakeRange(NSMaxrange(matchRange)+1,1)];                         }                     }];
解决方法 我建议您不要使用文本视图.你有一个列表,所以使用表或集合视图更适合这个用例.它们将使处理设备宽度和项目选择变得有效.它们还允许您在项目周围添加其他样式(这可能很方便).

我以前不需要这样做,但是目前我用文本视图我想你需要看看文本是如何放入布局的,我希望你需要明确地进行中心对齐,这样你就可以了可以选择任一侧的填充,而无需级联到上一行或下一行.这远非微不足道.

我很想知道文本视图问题是否有一个简单的解决方案,但它看起来似乎是用于您正在寻找的数据,显示和用户交互的错误视图.

总结

以上是内存溢出为你收集整理的ios – 在UITextView中突出显示一行全部内容,希望文章能够帮你解决ios – 在UITextView中突出显示一行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1003026.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存