更改UITextView中一个链接的属性 [英] Change attributes of one link in UITextView
问题描述
我有一个UITextView
,其中有多个URL,可以通过将dataDetectorTypes
属性设置为UIDataDetectorTypeLink
来激活.然后,我使用linkTextAttributes
属性设置链接的颜色.现在,当用户点击一个链接(使用UITapGestureRecognizer
)时,我只想更改该链接的颜色.如果我更改linkTextAttributes
,则所有链接都会更改颜色.
I have a UITextView
with multiple URLs that I activate by setting the dataDetectorTypes
property to UIDataDetectorTypeLink
. I then use the linkTextAttributes
property to set the color of the links. Now when the user taps on one of the links (using a UITapGestureRecognizer
), I'd like to change the color of that link only. If I change linkTextAttributes
, all the links will change color.
如何仅更改被点击链接的颜色?
How can I change just the color of the link that was tapped on?
推荐答案
我想我通过使用UITextView
的具有rangeOfLink
属性的子类解决了这个问题.
I think I solved it, using a subclass of UITextView
called which has a rangeOfLink
property.
首先,在我的UIViewController
viewDidLoad:
中,添加
First, in my UIViewController
viewDidLoad:
, I add
self.textView.dataDetectorTypes = UIDataDetectorTypeLink; // change for other link types
self.textView.selectable = YES;
self.textView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap:)];
tapGesture.cancelsTouchesInView = YES;
[self.textView addGestureRecognizer: tapGesture];
[self.textView setNeedsDisplay]; // force a redraw so that drawRect is called
然后在handleTap
中,我这样做:
MyTextViewWithLink *aTextView = (IDTextViewWithLink *) recognizer.view;
if (aTextView != self.textView)
return;
if (recognizer.state == UIGestureRecognizerStateEnded)
{
CGPoint location = [recognizer locationInView: aTextView];
// this returns an NSTextCheckingResult if location is inside a link
NSTextCheckingResult *result = [self textCheckingResultAtPoint: location inTextView: aTextView];
if (result)
{
aTextView.rangeOfLink = result.range;
[aTextView setNeedsDisplay]; // this will force the color change
// open url
}
}
最后,我在UITextView
子类中覆盖了drawRect
:
Finally I override drawRect
in my UITextView
subclass:
self.linkTextAttributes = [NSDictionary dictionary];
NSError *error = nil;
NSDataDetector *dataDetector = [NSDataDetector dataDetectorWithTypes: NSTextCheckingTypeLink error: &error]; // change for other link types
if (!error && dataDetector)
{
NSArray* resultString = [dataDetector matchesInString: self.text
options: NSMatchingReportProgress
range: NSMakeRange(0, [self.text length])];
if (resultString.count > 0)
{
NSMutableAttributedString *mas = [self.attributedText mutableCopy];
for (NSTextCheckingResult* result in resultString)
{
if (result.resultType == NSTextCheckingTypeLink)
{
NSRange intersection = NSIntersectionRange(result.range, self.rangeOfLink);
if (intersection.length <= 0) // no match
[mas addAttribute: NSForegroundColorAttributeName
value: [UIColor blueColor]
range: self.rangeOfLink];
else
[mas addAttribute: NSForegroundColorAttributeName
value: [UIColor redColor]
range: self.rangeOfLink];
}
}
self.attributedText = mas;
}
}
[super drawRect: rect];
现在,如果textView具有多个链接,则只有选定的链接会更改颜色.
Now if the textView has more than one link, only the selected one will change color.
这篇关于更改UITextView中一个链接的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!