2012-01-04 82 views
10

我已经使用“注册自己”作为文本。我想把它作为链接。并通过点击该链接,它应该打开Register.xib要将标签文本设为链接

如何才能得到这个链接?

+0

不能得到你的意思。你的意思是如何在文本上制作一个超链接,然后点击时,转到注册视图? – Raptor 2012-01-04 08:59:57

+5

最好使用自定义类型的UIbutton而不是lablel。 – 2012-01-04 09:05:38

+0

听起来像这样。 http://stackoverflow.com/questions/4743457/how-to-customize-uilabel-clickable – Ilanchezhian 2012-01-04 09:21:58

回答

12

贾巴尔是正确的。创建一个UIButton对象,将其类型设置为“Custom”,然后可以给它一个标题。将动作连接到推送或呈现您的注册视图控制器的方法,您将全部设置。

你可能想给一些指示该文本是一个可点击的链接的用户。将文本的颜色设置为蓝色。另一个完全独立的选项是使用NSAttributedString(具有下划线属性)向用户指示文本是可点击的。这需要an Open Source replacement for UITextView(您可以从this related question了解更多信息)。

+0

嘿所有,卡里姆,迈克尔,非常感谢....但我有另一个查询,我找不到任何方法来指示用户文本是可点击的,即有文本的蓝色。另一件事,我可以有“手”符号,当用户移动光标在该字符串.. ??? – Krunal 2012-01-04 09:55:03

+0

我想你是要求iPhone应用程序和iPhone应用程序不会显示任何光标在屏幕上,对不对? – DivineDesert 2012-01-04 12:28:00

+0

Oopss ...是的,我知道了... – Krunal 2012-01-04 12:44:42

0

首先,构建属性串并保存交互式文本和文本本身的范围。

然后,绘制属性串与CoreText框架,保持CTFrameRef。

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString); 
CGMutablePathRef path = CGPathCreateMutable(); 
CGPathAddRect(path, NULL, bounds); 

CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); 
CFRelease(framesetter); 

CTFrameDraw(frame, context); 
//CFRelease(frame); 

CGPathRelease(path); 

最后,重载[查看touchesEnded:(NSSet中*)触及withEvent:方法(的UIEvent *)事件]是这样的:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSSet *allTouches = [event allTouches]; 
    if ([allTouches count] == 1) { 
     UITouch *touch = (UITouch *)[allTouches anyObject]; 
     CGPoint touchPoint = [touch locationInView:self]; 

     // Convert to coordinate system of current view 
     touchPoint.y -= self.bounds.size.height; 
     touchPoint.y *= -1; 

     CGPathRef statusPath = CTFrameGetPath(statusCTFrame); 
     NSString *touchedInterStr = nil; 
     if (CGPathContainsPoint(statusPath, NULL, touchPoint, FALSE)) { 
      touchedInterStr = [self getInteractiveStringInFrame:statusCTFrame atPosition:touchPoint]; 
     } else { 
      return ; 
     } 
    } 
} 

- (NSString *)getInteractiveStringInFrame:(CTFrameRef)frame atPosition:(CGPoint)point 
{ 
    CGPathRef path = CTFrameGetPath(frame); 
    CGRect rect; 
    CGPathIsRect(path, &rect); 

    // Convert point into rect of current frame, to accurately perform hit testing on CTLine 
    CGPoint pointInsideRect = point; 
    pointInsideRect.x = point.x - rect.origin.x; 

    CFArrayRef lines = CTFrameGetLines(statusCTFrame); 
    CFIndex count = CFArrayGetCount(lines); 
    CGFloat curUpBound = rect.origin.y + rect.size.height; 
    CFIndex touchedIndex = -1; 
    for (CFIndex pos = 0; pos < count; pos++) { 
     CTLineRef curLine = CFArrayGetValueAtIndex(lines, pos); 
     CGFloat ascent, descent, leading; 
     CTLineGetTypographicBounds(curLine, &ascent, &descent, &leading); 
     CGFloat curHeight = ascent + descent + leading; 
     if (pointInsideRect.y >= curUpBound-curHeight && pointInsideRect.y <= curUpBound){ 
      touchedIndex = CTLineGetStringIndexForPosition(curLine, pointInsideRect); // Hit testing 
      break; 
     } 
     curUpBound -= curHeight; 
    } 
    if (touchedIndex == -1) 
     return nil; 

    NSEnumerator *enumerator = [interactiveStrs objectEnumerator]; 
    InteractiveString *curString; 
    while ((curString = (InteractiveString *)[enumerator nextObject])) { 
     if (NSLocationInRange(touchedIndex, curString.range)) { 
      return curString.content; 
     } 
    } 

    return nil; 
} 

你得到touchesEnded交互式文本,那么你可以做任何你想做的事情,比如触发委托方法。

PS:这是旧时尚的解决方案,我听说的iOS5已经像来实现需求的增强的UIWebView提供的东西,也许你可以检查苹果的SDK文档库。

+0

什么是“InteractiveString”我找不到它.... – 2012-05-31 06:24:45

2

我认为下面你会得到你想要的...

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSSet *allTouches = [event allTouches]; 
    if ([allTouches count] == 1) 
    { 
     UITouch *touch = (UITouch *)[allTouches anyObject]; 
     CGPoint touchPoint = [touch locationInView:self]; 

     // Convert to coordinate system of current view 
     touchPoint.y -= self.bounds.size.height; 
     touchPoint.y *= -1; 

     CGPathRef statusPath = CTFrameGetPath(statusCTFrame); 
     NSString *touchedInterStr = nil; 
     if (CGPathContainsPoint(statusPath, NULL, touchPoint, FALSE)) 
     { 
      touchedInterStr = [self getInteractiveStringInFrame:statusCTFrameatPosition:touchPoint]; 
     } 
     else 
     { 
      return ; 
     } 
    } 
} 
2

有一个中间地带是值得一提。使用UIButtonUIButtonTypeCustom有一些缺点,即您对文本样式的控制有限(例如,您无法使其右对齐或左对齐,它总是居中)。

使用NSAttributedString可以矫枉过正,而且很难实现,如果你不熟悉NSAttributedString,CoreText等

我使用的解决方案是UIControl一个简单的子类,允许更多的定制比继承UIButton少麻烦比NSAttributedString等。 UIControl方法只适用于整个字符串是“链接”的情况。

你会做这样的事情在你的UIControl子类:

  • 添加UILabel子视图在init方法
  • 配置UILabel与所需backgroundColortextColortextAlignment等,包括更改UILabel如果您希望背景显示为其iOS应用中Twitter的推文链接
  • 覆盖setHighlighted:方法以自定义外观ð觉得触摸更改/跟踪
  • 添加@property制定者在.h文件,以便所属的类可以设置状态
0

以一个按钮,并要设置为超链接文本按钮设置标题。您可以在iOS 7和iOS 8中选择默认和自定义按钮类型按钮,但对于iOS 7以下的版本,您只需要按钮类型自定义。您可以更改文本的颜色以突出显示用户界面的文本,并添加您希望添加到该按钮上的任何操作。它会像魅力一样运作,祝你好运。