我正在尝试在UITextView
中找到Tapped字符串。我试过了UIDataDetectorTypeLink
。但它没有用。我也不确定是否有可能检测到String
。我在自定义UITextView
中使用UITableViewCell
。从那我我试图检测轻拍的字符串。以下是我试过的示例代码。任何人都可以帮我找到我做错了什么,我怎么能解决这个问题?
即使不可能,我该如何解决这个问题呢?
NSMutableArray *sampleArray = [[NSMutableArray alloc]init];
NSMutableString *mutableString = [[NSMutableString alloc]init];
NSMutableAttributedString * string;
sampleArray = [[postArray valueForKey:@"hash_tags"] objectAtIndex:indexPath.row];
for(NSString *sampleString in sampleArray){
if (mutableString.length == 0) {
[mutableString appendString:sampleString];
}else{
[mutableString appendString:[NSString stringWithFormat:@", %@",sampleString]];
}
string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",mutableString]];
// [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,[string length])];
}
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor colorWithRed:5.0f/255.0f green:107.0f/255.0f blue:153.0f/255.0f alpha:1.0f], NSUnderlineColorAttributeName: [UIColor greenColor],NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
[string addAttribute:NSLinkAttributeName
value:string
range:NSMakeRange(0, [string length])];//[[string string] rangeOfString:sampleString]];
[cell.tagsTextView setAttributedText:string];
[cell.tagsTextView setDelegate:self];
[cell.tagsTextView setUserInteractionEnabled:YES];
cell.tagsTextView.scrollEnabled = NO;
cell.tagsTextView.editable = NO;
cell.tagsTextView.dataDetectorTypes = UIDataDetectorTypeLink;
// cell.tagsTextView.textContainer.lineFragmentPadding = 0;
// cell.tagsTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
[cell.tagsTextView setLinkTextAttributes:linkAttributes];
UITextView Delegates
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
NSLog(@"url %@",URL);
if ([[URL scheme] isEqualToString:@"username"]) {
NSString *username = [URL host];
// do something with this username
// ...
return NO;
}
return YES; // let the system open this URL
}
提前致谢
答案 0 :(得分:11)
将 UITapGestureRecognizer
添加到 UITextView
,下面是 UITapGestureRecognizer
选择器方法。< / p>
- (void) tapResponse:(UITapGestureRecognizer *)recognizer{
UITextView *textView = (UITextView *)recognizer.view;
CGPoint location = [recognizer locationInView:textView];
NSLog(@"Tap Gesture Coordinates: %.2f %.2f -- %@", location.x, location.y,textView.text);
CGPoint position = CGPointMake(location.x, location.y);
//get location in text from textposition at point
UITextPosition *tapPosition = [textView closestPositionToPoint:position];
//fetch the word at this position (or nil, if not available)
UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
NSString *tappedSentence = [textView textInRange:textRange];//[self lineAtPosition:CGPointMake(location.x, location.y)];
NSLog(@"selected :%@ -- %@",tappedSentence,tapPosition);
}
答案 1 :(得分:1)
感谢@Grey_Code,此代码的Swift 4版本为:
class Tender(Procurement):
pass
class OfferRequest(Procurement):
tender = models.ForeignKey(Tender)
@classmethod
def from_tender(cls, tender):
instance = cls()
instance.tender = tender
for field in Procurement._meta.fields:
if field.auto_created:
continue
setattr(
instance,
field.name,
getattr(tender, field.name, None)
)
instance.save()
return instance
tender = Tender.objects.first()
offer_request = OfferRequest.from_tender(tender)