Ios 将轻触手势添加到UILabel的一部分
我有一个NSAttribute字符串,如下所示:Ios 将轻触手势添加到UILabel的一部分,ios,iphone,uilabel,nsattributedstring,uitapgesturerecognizer,Ios,Iphone,Uilabel,Nsattributedstring,Uitapgesturerecognizer,我有一个NSAttribute字符串,如下所示: NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"testing it out @clickhere"]; NSInteger length = str.length; [str addAttribute:NSForegroundColorAttributeName value:[UIColor bestTextColor] r
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"testing it out @clickhere"];
NSInteger length = str.length;
[str addAttribute:NSForegroundColorAttributeName value:[UIColor bestTextColor] range:NSMakeRange(0,length)];
NSMutableAttributeString设置为UILabel,如下所示:
label.attributedText = str;
对于上面字符串中的“@clickhere”,如何对另一个viewcontroller进行点击手势(或可点击的东西)
谢谢 只需先在标签上添加一个手势即可
[label setUserInteractionEnabled:YES];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[label addGestureRecognizer:gesture];
let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction))
yourLabel.addGestureRecognizer(tap)
用下面的方法控制你的手势区域
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
static CGRect touchableRect = CGRectMake(100.0f, 0.0f, 100.0f, 50.0f); // Give your rect as you need.
CGPoint touchPoint = [gestureRecognizer locationInView:self.view];
if (CGRectContainsPoint(touchableRect, touchPoint))
{
//User has tap on where you want. Do your other stuff here
}
}
我认为,最好的方法是将
uigesturecognizer
添加到您的UILabel
中,并验证您想要的帧
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[_yourLabel addGestureRecognizer:singleTap];
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: _yourLabel];
//Modify the validFrame that you would like to enable the touch
//or get the frame from _yourLabel using the NSMutableAttributedString, if possible
CGRect validFrame = CGRectMake(0, 0, 300, 44);
if(YES == CGRectContainsPoint(validFrame, touchPoint)
{
//Handle here.
}
}
我只想补充Ramshad的答案,关于如何处理有效帧
为此,您可能需要考虑使用UITExtVIEW,而不是UIABEL,这不能让您访问它如何管理文本布局。通过禁用编辑、选择和滚动,UITextView的行为与UILabel大致相同,只是需要删除一些填充
为了方便起见,您可能希望向UITextView添加一个小类别,在该类别中,您可以编写一个方法来测试一个点是否触及范围内的任何字符- (BOOL)point:(CGPoint)point touchesSomeCharacterInRange:(NSRange)range
{
NSRange glyphRange = [self.layoutManager glyphRangeForCharacterRange:range actualCharacterRange:NULL];
BOOL touches = NO;
for (NSUInteger index = glyphRange.location; index < glyphRange.location + glyphRange.length; index++) {
CGRect rectForGlyphInContainer = [self.layoutManager boundingRectForGlyphRange:NSMakeRange(index, 1) inTextContainer:self.textContainer];
CGRect rectForGlyphInTextView = CGRectOffset(rectForGlyphInContainer, self.textContainerInset.left, self.textContainerInset.top);
if (CGRectContainsPoint(rectForGlyphInTextView, point)) {
touches = YES;
break;
}
}
return touches;
}
-(BOOL)点:(CGPoint)点接触someCharacterInRange:(NSRange)范围
{
NSRange glyphRange=[self.layoutManager glyphRangeForCharacterRange:range-actualCharacterRange:NULL];
布尔接触=否;
对于(整数索引=glyphRange.location;索引
这也适用于包含多个单词的文本片段,这些单词由于换行而分散在多行中。当我们处理打印的字形时,它也可以处理本地化文本。这个想法与公认的答案相同。这是斯威夫特的路
- 假设您已准备好设置标签
youLabel.text=“请为我制表!”
- 将TapGestuerRecognitor添加到标签
[label setUserInteractionEnabled:YES]; UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [label addGestureRecognizer:gesture];
let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction)) yourLabel.addGestureRecognizer(tap)
- 将
扩展方法添加到String以计算String rectString
extension String { func rect(font: UIFont) -> CGRect { let label = UILabel(frame: .zero) label.font = font label.text = self label.sizeToFit() return label.frame } }
- 计算点击手势的可用点击矩形
let pleaseStringRect = "please".rect(font: yourFont) let tapStringRect = "tap".rect(font: yourFont) let tapAreaRect = CGRect(x: pleaseStringRect.width, y: tapStringRect.origin.y, width: tapStringRect.width, height: tapStringRect.height"
- 在行动中做你想做的
就这样,快乐编码@objc private func tapAction(tap: UITapGestureRecognizer) { let position = tap.location(in: content) guard reporterFirstNameRect.contains(position) else { return } // Do the tap action stuff }