How do I add tab stops to an NSAttributedString and display in a UITextView

后端 未结 4 1890
一整个雨季
一整个雨季 2020-12-30 13:00

I\'m creating an iOS app, and I would like to display an attributed string with specific tab stops specified in a UITextView. I would also like to draw them directly into U

4条回答
  •  梦毁少年i
    2020-12-30 13:26

    This is the Swift version that worked for me:

        let tablInterval: CGFloat = 85.0
        let paragraphStyle = NSMutableParagraphStyle()
        let terms = NSTextTab.columnTerminatorsForLocale(NSLocale.currentLocale())
        let tabStop0 = NSTextTab(textAlignment: .Right, location: 0, options: [NSTabColumnTerminatorsAttributeName:terms])
        let tabStop1 = NSTextTab(textAlignment: .Right, location: tablInterval, options: [NSTabColumnTerminatorsAttributeName:terms])
        let tabStop2 = NSTextTab(textAlignment: .Right, location: tablInterval*2, options: [NSTabColumnTerminatorsAttributeName:terms])
        let tabStop3 = NSTextTab(textAlignment: .Right, location: tablInterval*3, options: [NSTabColumnTerminatorsAttributeName:terms])
        paragraphStyle.addTabStop(tabStop0)
        paragraphStyle.addTabStop(tabStop1)
        paragraphStyle.addTabStop(tabStop2)
        paragraphStyle.addTabStop(tabStop3)
    
        let attributedString = NSMutableAttributedString(string:text)
        attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:rangeAll)
    

提交回复
热议问题