【问题标题】:What is the equivalent of Android's "Spannable" in Objective-C什么是Objective-C中Android的“Spannable”等价物
【发布时间】:2013-12-01 09:22:06
【问题描述】:

我正在使用iOS 应用程序,该应用程序应该能够突出显示文本并使其也可点击。

我在 iOS 中读到了 NSAttributedString,但它仍然比 Android 中的 Spannable 复杂。

如果没有,还有其他Objective c 的方法吗?我应该如何使用NSAttributedString 逐字突出显示段落,以及如何使我的文本可点击。

更新:

我到底想要什么,每个词都应该是可点击的,并且可以 在一个段落中突出显示为一个单词

【问题讨论】:

  • 最简单的方法可能是在 UIWebView 中加载 HTML
  • 我想在本地做,使用objective c
  • 不,它不是重复的,因为在搜索 Spannable 等价物(每个单词应该是可点击的并且可以突出显示为单个单词)时,请阅读问题!!!!
  • 如果您阅读了答案,其中有很多提示可以提供帮助。还是您只是在等待有人给您所需的几行代码?

标签: ios objective-c core-text nsattributedstring spannablestring


【解决方案1】:

我找到了一个使用UITextView 的完美解决方案,它可以让UITextView 中的每个单词都可以点击。

首先,创建一个UITextView 并添加一个UITapGestureRecognizer,如下所示:

CGRect textViewFrame = CGRectMake(0, 40, 100, 100);
textView = [[UITextView alloc]initWithFrame: textViewFrame];
textView.textAlignment = NSTextAlignmentCenter;
textView.backgroundColor = [UIColor clearColor];
textView.editable = NO;
textView.selectable = NO;

[self.view addSubView:textView];

// i used to `NSMutableAttributedString` highlight the text

 string = [[NSMutableAttributedString alloc]initWithString:@"Any text to detect A B $ & - +"];
    [string addAttribute:NSFontAttributeName
                  value:[UIFont systemFontOfSize:40.0]
                  range:NSMakeRange(0, [string length])];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
    [paragraphStyle setAlignment:NSTextAlignmentCenter];

    [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];

    [textView setAttributedText:string];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];

//modify this number to recognizer number of tap
[singleTap setNumberOfTapsRequired:1];
[textView addGestureRecognizer:singleTap];

然后添加UITapGestureRecognizer@selector:

- (void)tapRecognized:(UITapGestureRecognizer *)recognizer{

    if(recognizer.state == UIGestureRecognizerStateRecognized)
    {

        CGPoint point = [recognizer locationInView:recognizer.view];
        NSString * detectedText = [self getWordAtPosition:point inTextView: textView];

        if (![detectedText isEqualToString:@""]) {

    NSLog(@"detectedText  == %@", detectedText);


        } }

}

所有这些魔法都与这个方法有关,女巫可以检测到UITextView上的任何触摸并获得点击的单词:

-(NSString*)getWordAtPosition:(CGPoint)pos inTextView:(UITextView*)_tv
{
    //eliminate scroll offset
    pos.y += _tv.contentOffset.y;

    //get location in text from textposition at point
    UITextPosition *tapPos = [_tv closestPositionToPoint:pos];

    //fetch the word at this position (or nil, if not available)
    UITextRange * wr = [_tv.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];

    return [_tv textInRange:wr];
}

为了突出显示文本:

-(void)setTextHighlited :(NSString *)txt{

    for (NSString *word in [textView.attributedText componentsSeparatedByString:@" "]) {

        if ([word hasPrefix:txt]) {
            NSRange range=[self.textLabel.text rangeOfString:word];
            [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
         }}
    [textView setAttributedText:string];

}

就是这样,希望这对其他人有帮助。

【讨论】:

    【解决方案2】:

    不幸的是,您正在寻找的行为在 Objective-c 中并不像 Spannable 那样简单。

    但是,它可以相当容易地完成:

    1. 创建一个 UILabel 或 UITextView。
    2. 使用 NSMutableAttributedString 和 NSRange 突出显示一些单词(见下文)。
    3. 将属性字符串应用于属性文本属性。
    4. 创建具有清晰背景且没有标签和位置在可点击字词顶部的 UIButton。
    5. 为每个按钮添加一个目标并创建一个选择器方法。

    以下是属性字符串的一些示例代码,可帮助您入门:

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:  [NSString stringWithFormat:@"Text containing a bold word and another"];
    [string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14] range:NSMakeRange(18,4)];
    [string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14] range:NSMakeRange(32, 7)];
    label.attributedText = string; 
    

    【讨论】:

    • 我有1000多字,太重了!!
    • 在这种情况下,您只剩下 2 个选项:将您的文本格式化为 html 并使用 UIWebView 或查找已经有人解决此问题的库。
    猜你喜欢
    • 2018-04-19
    • 1970-01-01
    • 2014-07-28
    • 2015-07-30
    • 2011-07-21
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多