将 UIGestureRecognizer 添加到 UILabel 的单个字母 [英] Add UIGestureRecognizer to individual letters of UILabel

查看:25
本文介绍了将 UIGestureRecognizer 添加到 UILabel 的单个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在制作一个 iOS 应用程序,我想通过简单地删除字母"来实现删除 UILabel 中字母的功能.我遇到问题的部分是向 UILabel 的单个字母添加手势识别器.我已经在网上搜索过这个,一无所获.澄清一下,我不是在整个标签中添加手势识别器,我只想将其添加到单个字母中.非常感谢任何帮助.

Right Now I am making an iOS app and I would like to implement the ability to remove letters in a UILabel by simply "Dropping the letter". The part That I am having an issue with is adding a gesture recognizer to individual letters of a UILabel. I have already searched the web for this and have nothing.Just to be clear, I am NOT adding a gesture recognizer to the whole label, I am only wanting to add it to individual letters. Any help is greatly appreciated.

谢谢

推荐答案

似乎最简单的方法是动态计算字母的位置.使用这个:

It seems that the easiest way to do it is by dynamically calculating the position of a letter. Use this:

CGSize textSize = [text sizeWithFont:[UIFont boldSystemFontOfSize:size]
                   constrainedToSize:constrainedSize
                       lineBreakMode:NSLineBreakByWordWrapping];

因此您可以获取每个标签使用的字体和大小中的字母大小,并使用 [stringInstance length] 属性和 [UILabel numberOfLines] 属性来获取标签中每个字母的近似中心,然后对每个标签使用一个简单的 tapGestureRecognizer 并在那里调用您的方法

so you can get the the size for a letter in the font and size you are using for each label and using the [stringInstance length] property and the [UILabel numberOfLines] property to get the approximate center for each letter in a label, then use a simple tapGestureRecognizer for each label and in there calling your method for

- (NSString*)letterForPoint:(CGPoint)tapCenter inLabel:(UILabel*)label;

在那里你使用一切来计算每个字母的近似中心,并为错误和正确的用户响应添加一个可选范围,如 x +- 20 像素和 y +- 20 像素.

there you use everything to calculate the approximate center for each letter and adding a selectableRange for error and correct user responding as x +- 20 pixels and y +- 20 pixels.

Apple 表示,对于 40 像素的可选边界小于 40 像素的任何内容都会让用户感到厌烦,因此对于用户交互而言,您的字体大小实际上应该相当大.

Apple says that anything with a selectable bound lesser than 40 pixels for 40 pixels will be completely annoying for the user, so your font size should actually be quite big for user interaction.

这篇关于将 UIGestureRecognizer 添加到 UILabel 的单个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆