2016-06-28 94 views
4

我有一个UITextView,它有一个NSAttributedString,每行之间有两个换行符。我试图在textview中突出显示一行。我可以简单地在textview中突出显示一个句子,而不会有任何问题。但是,我需要突出显示从行首(textview的左侧)到行尾(textview的右侧)的句子在UITextView中突出显示一行

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

enter image description here

// 1. create some fonts 
UIFontDescriptor* 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 highlight 
NSString* 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 attributes 
NSDictionary* 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)]; 
         } 
        }]; 
+0

您的文字是否始终是购物清单,中间是否与这些回车符一致? – Wain

+0

@Wain不是购物清单,而是句子,中心与两个回车对齐。 –

+0

请问,您如何决定要突出显示哪条线? – Happiehappie

回答

1

我建议你不要使用这个文本视图。你有什么是一个列表,所以使用表或集合视图更适合这个用例。他们将使处理设备宽度和项目选择变得微不足道。他们还允许您在项目周围添加其他样式(这可能很方便)。

我以前没有必要这样做,但现在有了文本视图,我想你需要看看文本如何放置到布局中,我希望你需要做中心明确对齐,以便您可以选择任一侧的填充而不会级联到上一行或下一行。这远非微不足道。

我很想知道文本视图问题是否有一个简单的解决方案,但它确实看起来像用于您要查找的数据,显示和用户交互的错误视图。

+0

关于表视图的好建议,但由于某些原因,我必须坚持textview。因为我有选项来查找/替换,编辑文本... –

+0

我可以尝试明确地对齐中心的文本,就像你说的,但选择两边的填充是问题。我没有任何想法做到这一点。你可以帮我吗? –

+1

这很难,你需要计算出视图宽度上有多少个字符。文本搜索和编辑可以在表格视图中正常工作,表格甚至可以使用编辑模式,您可以使用单元格和单元格明确了解编辑... – Wain

-1

我不知道该如何解释这一点,但我认为它与\ n和\ r由iOS解释有关。

我猜你可能已经通过使用两个\ r或两个或者创建行差距\ n即

NSString *text = @"Shopping List\n\n1. Cheese\n\n2. Biscuits\r\r3 Sausages\n\n4. IMPORTANT Cash for going out!\n\n5. -potatoes-\n\n6. Acopy of iOS6 by tutorials\n\n7. A new iPhone\n\n8. A present for mum"; 

OR

NSString *text = @"Shopping List\n\n1. Cheese\n\n2. Biscuits\n\n3 Sausages\n\n4. IMPORTANT Cash for going out!\n\n5. -potatoes-\n\n6. Acopy of iOS6 by tutorials\n\n7. A new iPhone\n\n8. A present for mum"; 

两种方式你的代码要突出生产线生产与您的输出相同的输出。

但是,如果你更换了2号 '\ n' 或 '\ r' 与 '\ r \ n' 即

NSString *text = @"Shopping List\n\n1. Cheese\n\n2. Biscuits\r\r\n3 Sausages\n\n4. IMPORTANT Cash for going out!\n\n5. -potatoes-\n\n6. Acopy of iOS6 by tutorials\n\n7. A new iPhone\n\n8. A present for mum"; 

或者

NSString *text = @"Shopping List\n\n1. Cheese\n\n2. Biscuits\n\r\n3 Sausages\n\n4. IMPORTANT Cash for going out!\n\n5. -potatoes-\n\n6. Acopy of iOS6 by tutorials\n\n7. A new iPhone\n\n8. A present for mum"; 

那么你得到想要的结果。

+0

看起来你不明白我的问题。 –

+0

@RamarajT:当我使用“\ n \ r \ n”获得两个换行符而不是使用“\ n \ n”或“\ r \ r”时,我能够使用您的代码片段突出显示一行。 – SHN