代码之家  ›  专栏  ›  技术社区  ›  Piepants

属性化文本从uiview移到uiscrollview后不再工作

  •  -1
  • Piepants  · 技术社区  · 6 年前

    我有一个uitextview,其中部分文本是粗体的,并且在点击时有相关的URL来启动。 当uitextView是视图控制器主uiview的子级时,它工作得很好。

    但是,视图控制器的整体内容变得太大,因此我将视图和子视图的全部内容移动到uiscrollview中,即层次结构已从:

    View Controller
        View
           stuff
           UITextView
           stuff
    

    到:

    View Controller
      View
         Scroll View
            View
               stuff
               UITextView
               stuff
    

    但是,现在属性已经停止工作-粗体文本不会显示,并且当文本被点击时URL不会启动。 没有代码更改,所有更改仅限于情节提要和添加滚动视图。

    没有启用用户交互等没有问题,因为滚动视图会滚动,除了uitextview还有uibutton以及点击时的功能。

    当uiextview成为uiscrollview的子级时,它的属性为什么停止工作?

    属性设置如下:

    func setsubscribeText()
    {
        subscribeText.contentInset = UIEdgeInsets.zero
    
        let linkAttributes: [String : Any] = [
            NSAttributedStringKey.foregroundColor.rawValue: UIColor.black,
            NSAttributedStringKey.underlineColor.rawValue: UIColor.black,
            NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue]
        subscribeText.linkTextAttributes = linkAttributes
    
        subscribeText.text = NSLocalizedString("VC_COMMON_SUBSCRIBE_FULL_TEXT", comment: "")
        let theString = subscribeText.attributedText?.mutableCopy(with: nil) as! NSMutableAttributedString
    
        let subNowRange = theString.mutableString.range(of: NSLocalizedString("VC_COMMON_SUBSCRIBE_BOLD_TEXT", comment: ""))
        let fontSize    = subscribeText.font?.pointSize
        let attributes  = [NSAttributedStringKey.font : UIFont(name: "NHaasGroteskDSStd-65Md", size: fontSize!)!]
        theString.addAttributes(attributes, range: subNowRange)
    
        let tcRange = theString.mutableString.range(of: NSLocalizedString("VC_COMMON_SUBSCRIBE_TERMS_TEXT", comment: ""))
        theString.addAttribute(NSAttributedStringKey.link, value: Config.termsAndConditionsURL(), range: tcRange)
        theString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: tcRange)
    
        let ppRange = theString.mutableString.range(of: NSLocalizedString("VC_COMMON_SUBSCRIBE_PRIVACY_TEXT", comment: ""))
        theString.addAttribute(NSAttributedStringKey.link, value: Config.privacyPolicyURL(), range: ppRange)
        theString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: ppRange)
    
        subscribeText.attributedText = theString
    }
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   matt    6 年前

    你想结合 linkTextAttributes 手动设置链接的文本属性。你不能那样做。

    所以,你是说:

    subscribeText.linkTextAttributes = linkAttributes
    

    但你也在说:

    theString.addAttribute(NSAttributedStringKey.link, value: Config.privacyPolicyURL(), range: ppRange)
    theString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: ppRange)
    

    这两种设置链接样式的方法是相互不兼容的。