HeshamMegid / HMSegmentedControl

A highly customizable drop-in replacement for UISegmentedControl.
Other
4.02k stars 835 forks source link

NSAttributedString as section title crashes in Swift App #137

Closed benjaminhorner closed 9 years ago

benjaminhorner commented 9 years ago

I have tried to implement a section title in my Swift App as an NSAttributedString but I get the following crash : -[NSConcreteAttributedString _fastCharacterContents].

        let font: UIFont = UIFont(name: "Palatino-Roman", size: 24.0)!
        let attrsDictionary: NSDictionary = NSDictionary(object: font, forKey: NSFontAttributeName)

        let attributedString: NSAttributedString = NSAttributedString(string: "Testing Attributed Strings", attributes: attrsDictionary as [NSObject : AnyObject])

        // Section titles
        let sectionTitles = [attributedString, "22.2k \nQUESTIONS", "409  \nBOOKMARKS"]

        // Add custom segmented Control
        let segmentedControl: HMSegmentedControl = HMSegmentedControl(sectionTitles: sectionTitles)
brunojppb commented 9 years ago

I had the same issue. I solved this problem setting the title formatter block like that:

var titleFormatterBlock: HMTitleFormatterBlock = {(control: AnyObject!, title: String!, index: UInt, selected: Bool) -> NSAttributedString in
      let attrString = NSAttributedString(string: title, attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])
      return attrString
    }
self.segmentControl.titleFormatter = titleFormatterBlock
benjaminhorner commented 9 years ago

Thanks ! works like a charm