【问题标题】:Selection and highlighting text on a label选择和突出显示标签上的文本
【发布时间】:2011-09-14 00:51:03
【问题描述】:

我想选择然后用特定颜色突出显示标签上的相同文本。这可以通过手势来实现吗? 而且我必须存储突出显示部分的位置,即使应用程序终端,所以当用户回来时,他们可以看到突出显示的部分

谢谢

【问题讨论】:

    标签: iphone objective-c text highlighting gestures


    【解决方案1】:

    NSUserDefaults 不适合,因为应用程序可能会意外终止 UITapGestureRecognizer 不支持任何状态,除了UIGestureRecognizerStateEnded

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizerAction:)];
        longPressGestureRecognizer.minimumPressDuration = 0.01;
        [label setUserInteractionEnabled:YES];
        [label addGestureRecognizer:longPressGestureRecognizer];
    }
    
    
    - (void)longPressGestureRecognizerAction:(UILongPressGestureRecognizer *)gestureRecognizer
    {
        if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
        {
            label.alpha = 0.3;
        }
        else
        {
            label.alpha = 1.0;
    
            CGPoint point = [gestureRecognizer locationInView:label];
            BOOL containsPoint = CGRectContainsPoint(label.bounds, point);
    
            if (containsPoint)
            {
                // Action (Touch Up Inside)
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      是的,您可以通过更改UILabel 的背景颜色或文本颜色来使用UILabel 的手势来突出显示文本。

      您还可以使用 NSUserDefaults 存储您的UILabel当前状态,然后在我们用户启动您的应用程序时回读。

      isLabelHighlighted 声明为UILabel 状态的BOOL

      UITapGestureRecognizer* myLabelGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LabelClicked:)];
      [myLabelView setUserInteractionEnabled:YES];
      [myLabelView addGestureRecognizer:myLabelGesture];
      
      
      -(void)LabelClicked:(UIGestureRecognizer*) gestureRecognizer
      {
          if(isLabelHighlighted)
          { 
               myLabelView.highlightedTextColor = [UIColor greenColor];
          }
          else 
          {
               myLabelView.highlightedTextColor = [UIColor redColor];
          }
      }
      

      存储您的UILabel状态

      [[NSUserDefaults standardUserDefaults] setBool:isLabelHighlighted forKey:@"yourKey"];
      

      要访问它,你应该使用下面。

      isLabelHighlighted = [[NSUserDefaults standardUserDefaults] boolForKey:@"yourKey"];
      

      【讨论】:

      • 你得到答复了吗?请分享
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多