【问题标题】:To make label text as link将标签文本作为链接
【发布时间】:2015-09-21 13:29:00
【问题描述】:

我使用“注册自己”作为文本。我想把它作为链接。通过单击该链接,它应该会打开Register.xib

我怎样才能得到这个链接??

【问题讨论】:

  • 无法理解您的意思。你的意思是如何在文本上创建一个超链接,然后点击后进入注册视图?
  • 最好使用自定义类型的UIbutton而不是labell。
  • 在一个小的 UIWebView 中嵌入 HTML 怎么样?出于某种原因应该避免这种情况 - 还是不可能?

标签: ios


【解决方案1】:

卡里姆是对的。创建一个 UIButton 对象,将它的类型设置为“自定义”,然后你可以给它一个标题。将操作连接到推送或呈现您的注册视图控制器的方法,您将一切就绪。

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

【讨论】:

  • 我想你要的是 iphone 应用程序,而 iphone 应用程序不会在屏幕上显示任何光标,对吧?
【解决方案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 ;
        }
    }
}

【讨论】:

    【解决方案3】:

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

    如果您不熟悉 NSAttributedString、CoreText 等,使用 NSAttributedString 可能会有点矫枉过正,而且难以实施。

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

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

    • 在init方法中添加UILabel子视图
    • 使用您想要的backgroundColortextColortextAlignment 等配置UILabel,如果您希望背景看起来像 Twitter 在其 iOS 应用程序中的推文内链接,则包括对 UILabel 层的更改
    • 覆盖setHighlighted: 方法以自定义触摸/跟​​踪的外观变化
    • .h 文件中添加@property 设置器,以便所属类可以设置状态

    【讨论】:

      【解决方案4】:

      首先,构造属性字符串,保存交互文本的范围和文本本身。

      然后,用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);
      

      最后,像这样覆盖 [view touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event]:

      - (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之类的东西来实现需求,也许你可以查看apple sdk文档库。

      【讨论】:

        【解决方案5】:

        选择一个按钮并设置要设置为超链接文本的按钮的标题。您可以在 iOS 7 和 iOS 8 中选择默认和自定义按钮类型按钮,但对于 iOS 7 以下,您必须仅采用自定义按钮类型。您可以更改文本的颜色以突出显示 UI 的文本,然后添加您想要在该按钮单击时添加的任何操作。它会像魅力一样起作用,祝你好运。

        【讨论】:

          【解决方案6】:

          看看TTTAttributedLabel。配置链接、电话等真的很简单。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-04-07
            • 1970-01-01
            • 1970-01-01
            • 2018-11-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-14
            • 2022-01-24
            相关资源
            最近更新 更多