点击 UILabel 的部分文本

2024-02-25

我有一个问题boundingRectForGlyphRange总是返回CGRect.zero“0.0,0.0,0.0,0.0”。

例如,我正在编写触摸文本的一部分的代码UILabel特征。我的文字第一部分是any text第二个是阅读更多.

我希望点击识别器仅在我触摸时才起作用阅读更多。如果我触及任何一点UILabel, CGRectContainsPoint总是回来true,然后调用该操作。

这是我的代码:

override func viewDidLoad() {
        super.viewDidLoad()
        
        // The full string
        
        let firstPart:NSMutableAttributedString = NSMutableAttributedString(string: "Lorem ipsum dolor set amit ", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(13)])
        firstPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(),
            range: NSRange(location: 0, length: firstPart.length))
        info.appendAttributedString(firstPart)
        
        // The "Read More" string that should be touchable
        let secondPart:NSMutableAttributedString = NSMutableAttributedString(string: "READ MORE", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14)])
        secondPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(),
            range: NSRange(location: 0, length: secondPart.length))
        info.appendAttributedString(secondPart)
        
        lblTest.attributedText = info
        
        // Store range of chars we want to detect touches for
        moreStringRange = NSMakeRange(firstPart.length, secondPart.length)
        print("moreStringRange\(moreStringRange)")
        
        tapRec.addTarget(self, action: "didTap:")
        lblTest.addGestureRecognizer(tapRec)
        
    }


    func didTap(sender:AnyObject) {
        // Storage class stores the string, obviously
        let textStorage:NSTextStorage = NSTextStorage(attributedString: info)
        // The storage class owns a layout manager
        let layoutManager:NSLayoutManager = NSLayoutManager()
        textStorage.addLayoutManager(layoutManager)
        
        // Layout manager owns a container which basically
        // defines the bounds the text should be contained in
        let textContainer:NSTextContainer = NSTextContainer(size: lblTest.frame.size)
        textContainer.lineFragmentPadding = 0
        textContainer.lineBreakMode = lblTest.lineBreakMode
        
        // Begin computation of actual frame
        // Glyph is the final display representation
        var glyphRange = NSRange()
        // Extract the glyph range
        layoutManager.characterRangeForGlyphRange(moreStringRange!, actualGlyphRange: &glyphRange)
        
        // Compute the rect of glyph in the text container
        print("glyphRange\(glyphRange)")
        print("textContainer\(textContainer)")
        let glyphRect:CGRect = layoutManager.boundingRectForGlyphRange(glyphRange, inTextContainer: textContainer)
        
        // Final rect relative to the textLabel.
        print("\(glyphRect)")
        
        // Now figure out if the touch point is inside our rect
        let touchPoint:CGPoint = tapRec.locationOfTouch(0, inView: lblTest)
        
        if CGRectContainsPoint(glyphRect, touchPoint) {
            print("User tapped on Read More. So show something more")
        }
    }
}

#迅速4.2 请在此处找到获取特定文本的解决方案action of Label.

  1. 标签声明

    @IBOutlet weak var lblTerms: UILabel!
    
  2. 将属性文本设置为标签

    let text = "Please agree for Terms & Conditions."
    lblTerms.text = text
    self.lblTerms.textColor =  UIColor.white
    let underlineAttriString = NSMutableAttributedString(string: text)
    let range1 = (text as NSString).range(of: "Terms & Conditions.")
         underlineAttriString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range1)
         underlineAttriString.addAttribute(NSAttributedString.Key.font, value: UIFont.init(name: Theme.Font.Regular, size: Theme.Font.size.lblSize)!, range: range1)
         underlineAttriString.addAttribute(NSAttributedString.Key.foregroundColor, value: Theme.color.primaryGreen, range: range1)
    lblTerms.attributedText = underlineAttriString
    lblTerms.isUserInteractionEnabled = true
    lblTerms.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(tapLabel(gesture:))))
    

它看起来像上图。

  1. Add the tapLabel控制器的动作方法

    @IBAction func tapLabel(gesture: UITapGestureRecognizer) {
    let termsRange = (text as NSString).range(of: "Terms & Conditions")
    // comment for now
    //let privacyRange = (text as NSString).range(of: "Privacy Policy")
    
    if gesture.didTapAttributedTextInLabel(label: lblTerms, inRange: termsRange) {
        print("Tapped terms")
    } else if gesture.didTapAttributedTextInLabel(label: lblTerms, inRange: privacyRange) {
        print("Tapped privacy") 
    } else {                
        print("Tapped none")
    }
    }
    
  2. Add UITapGestureRecognizer扩大

    extension UITapGestureRecognizer {
    
        func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool {
            // Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
            let layoutManager = NSLayoutManager()
            let textContainer = NSTextContainer(size: CGSize.zero)
            let textStorage = NSTextStorage(attributedString: label.attributedText!)
    
            // Configure layoutManager and textStorage
            layoutManager.addTextContainer(textContainer)
            textStorage.addLayoutManager(layoutManager)
    
            // Configure textContainer
            textContainer.lineFragmentPadding = 0.0
            textContainer.lineBreakMode = label.lineBreakMode
            textContainer.maximumNumberOfLines = label.numberOfLines
            let labelSize = label.bounds.size
            textContainer.size = labelSize
    
            // Find the tapped character location and compare it to the specified range
            let locationOfTouchInLabel = self.location(in: label)
            let textBoundingBox = layoutManager.usedRect(for: textContainer)
            //let textContainerOffset = CGPointMake((labelSize.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x,
            //(labelSize.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y);
            let textContainerOffset = CGPoint(x: (labelSize.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x, y: (labelSize.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y)
    
            //let locationOfTouchInTextContainer = CGPointMake(locationOfTouchInLabel.x - textContainerOffset.x,
            // locationOfTouchInLabel.y - textContainerOffset.y);
            let locationOfTouchInTextContainer = CGPoint(x: locationOfTouchInLabel.x - textContainerOffset.x, y: locationOfTouchInLabel.y - textContainerOffset.y)
            let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
            return NSLocationInRange(indexOfCharacter, targetRange)
        }
    }
    

确保执行以下操作:

lblTerms.isUserInteractionEnabled = true
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

点击 UILabel 的部分文本 的相关文章

  • 防止点击 MKAnnotation 时检测到 MKMapView 上的触摸事件

    我有一个 UITapGestureRecognizer 当用户点击地图时 它将在我的 MKMap 上隐藏和显示工具栏 简单 但是 当用户点击 MKMapAnnotation 时 我不希望地图以正常方式响应点击 如上所述 此外 当用户点击地图
  • 如何无限地每1分钟运行一个iOS应用程序?

    我制作了一个应用程序 需要每 1 分钟向服务器发送一次位置和状态更新 我尝试了以下方法 但没有一个能帮助我 有什么解决办法吗 1 NSTimer 很多人建议这样做 但问题出在后台模式上 它只能工作 20 分钟 该应用程序停止发送数据后 2
  • Android - API 请求

    我开发了一个应用程序 它也在 iPhone 上 问题出在 api 请求上 我为所有请求设置了超时 有时会出现 30 60 秒的中断 看起来这个应用程序执行了几个请求 然后就中断了 一直超时 大约 45 秒后一切正常 不知道是服务器问题还是安
  • 在 iOS 上使用 HEVC 编码器输出视频尺寸巨大

    我有一个项目 目前使用 H 264 编码器在 iOS 上录制视频 我想尝试在 iOS 11 中使用新的 HEVC 编码器来减小文件大小 但发现使用 HEVC 编码器会导致文件大小急剧膨胀 GitHub 上的一个项目显示了该问题 它使用 H
  • 如何在 NSDate 变量中仅设置时间?

    我有 NSDate 变量 只想更改时间 不应更改日期 是否可以 例如 用户在 DatePicker 日期中选择日期间隔 如果是开始日期 我想将时间设置为 00 00 00 如果是结束日期 我将时间设置为 23 59 59 感谢你的帮助 问候
  • 如何在 SQLite 中替换字符串?

    如何更新具有以下内容的表列 var mobile 233KKFSDK3234 Documents Page jpg 并将其替换为 Documents Page jpg 在 SQLite 中 注意 所有文字 除了 Documents 是动态的
  • 以编程方式向 UIButton 标签添加阴影

    我试图向按钮标签添加 1px 黑色阴影 但没有成功 我试过这个 self setTitleShadowOffset CGSizeMake 0 1 但我得到 请求非结构或联合中的成员 setTitleShadowOffset 任何建议都会很棒
  • AppDelegate 的变量用作全局变量不起作用

    我想使用我的 AppDelegate 来存储任何其他类都可以访问的对象 我已经像这样声明了这个 AppDelegate interface MyAppDelegate UIResponder
  • 如何从日期中获取小时、分钟和上午/下午? [复制]

    这个问题在这里已经有答案了 我尝试从日期中提取小时 分钟和上午 下午 但我得到 NULL 输出 我在下面显示了我的代码 请查看 NSString dateStr 29 07 2013 02 00am NSDateFormatter form
  • 无法添加钥匙串项目。使用 KeychainItemWrapper 更改标识符后出现错误 - 25299?

    我想用 KeychainItemWrapper 将 UUID 保存在钥匙串中 所以我在中添加以下方法MyKeychainManager m define keychain idenentify com myapp bundle1 void
  • CMSampleBufferSetDataBufferFromAudioBufferList 返回错误 12731

    我正在尝试捕获应用程序声音并将其传递给 AVAssetWriter 作为输入 我正在设置音频单元的回调以获取 AudioBufferList 问题始于将 AudioBufferList 转换为 CMSampleBufferRef 它总是返回
  • 如何将 UIImageView 裁剪为自定义形状

    用户是否可以在该位周围画一条虚线 圆圈 UIImageView他们希望裁剪到 然后为UIImageView调整大小到这些点 这有点像 Photoshop 中的套索 选取框效果 更新 从 iOS 8 x 开始 UIImageView 提供了m
  • 无需 iPhone 6 Plus 即可预览 iOS 应用程序

    我已经在 Yosemite 中使用 iPhone 6 和 Quicktime 完成了 iOS 应用程序预览 视频 但我无法在 iTunes Connect 中为 iPhone 6 上传它 而且我没有 iPhone 6 设备 有没有办法在没有
  • ShareKit + Facebook 身份验证不起作用

    我正在使用 ShareKit 从我正在开发的 PhoneGap 应用程序发布到 Facebook 我在 Facebook 中创建了该应用程序 并在我的项目中安装了该插件 Twitter 工作正常 但当我尝试在 Facebook 上分享时出现
  • iOS 设置 MKMapView 中心,因此提供的位置位于底部中心

    我有一个 MKMapView 和一个永不改变的 CLLocationCooperative2D 我想做的是将地图居中 以便该坐标将放置在地图的底部中心 我可以用简单的方法将地图集中在这个坐标上 MKCoordinateRegion view
  • 如何修复 ReactiveCocoa(带有 CocoaPods)的安装?

    这里是新手 尝试将 ReactiveCocoa 2 2 4 和 CocoaPods 安装到项目中 但在运行时遇到错误 我用过github 上的这个 podspec https github com CocoaPods Specs blob
  • 我如何从 iPhone 设备获取电子邮件历史记录..?

    friends 我想从我的 iPhone 访问电子邮件历史记录 并且还希望在收到新邮件时收到通知 如果可能的话 请向我提供源代码片段 Thanks 简而言之 使用任何已记录的 API 都是不可能的
  • 将捕获的图像精确裁剪为 AVCaptureVideoPreviewLayer 中的外观

    我有一个使用 AV Foundation 的照片应用程序 我使用 AVCaptureVideoPreviewLayer 设置了一个预览层 它占据了屏幕的上半部分 因此 当用户尝试拍照时 他们只能看到屏幕上半部分看到的内容 这很好用 但是当用
  • 断点条件错误

    我已经根据条件设置了断点 event name isEqualToString Some Name 这很好用 但是 当我尝试添加另一个带有条件的断点时 part name isEqualToString Some Value With A
  • 进入后台时 Alamofire 请求卡住?

    我正在使用 Alamofire 调用 Web 服务 该服务需要相当长的时间才能加载 如果应用程序进入后台 当我返回应用程序时 我会被加载程序卡住 我想这是因为调用永远不会向我的完成处理程序返回任何内容 我该如何解决这个问题 您可以使用后台抓

随机推荐