2011-06-13 43 views
5

我想選擇標籤上的相同文本並使用特定顏色突出顯示。使用手勢可以實現這一點。 我必須存儲高亮部分的位置,即使在應用程序terminas,因此當用戶回來,他們可以看到部分突出選擇並突出顯示標籤上的文字

感謝

回答

6

是,你可以使用手勢與您的UILabel通過更改UILabel的背景顏色或文本顏色來突出顯示文本。

您還可以存儲當前狀態UILabel使用NSUserDefaults,並讀回我們用戶啓動應用程序。

聲明isLabelHighlightedBOOLUILabel狀態。

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"]; 
+1

其working.But我將如何選擇標籤上的特定文本。 – Rachit 2011-06-14 13:51:15

+0

你有回答這個嗎?請分享 – 2015-04-27 11:50:24

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) 
     } 
    } 
}