选择并突出显示标签上的文本

发布于 2024-11-15 08:18:32 字数 100 浏览 2 评论 0原文

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

谢谢

I want to select and then highlight the same text on the label with a particular color.Is this be achievable with the help of gestures.
And i have to store the position of the highlighted part,even if the application terminas,so when the user comes back ,they can see that part highlighted

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

林空鹿饮溪 2024-11-22 08:18:33

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

您还可以使用 NSUserDefaults 存储 UILabel当前状态,并在用户启动您的应用程序时读取它。

UILabel 状态的 isLabelHighlighted 声明为 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"];

Yes, you could use the gesture with your UILabel for highlighting text by either changing the background color or text color of your UILabel.

You could also store the current state of your UILabel using NSUserDefaults , and read it back we user launch your application.

Declare an isLabelHighlighted as BOOL for UILabel state.

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];
    }
}

To store state of your UILabel.

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

To access it, you should use below.

isLabelHighlighted = [[NSUserDefaults standardUserDefaults] boolForKey:@"yourKey"];
空城仅有旧梦在 2024-11-22 08:18:33

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

NSUserDefaults is not suitable, because the application can be terminated unexpectedly
UITapGestureRecognizer not supported any states, except 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)
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文