Attributed Text Center Alignment

前端 未结 10 2457
故里飘歌
故里飘歌 2020-12-13 17:06

I have tried everything but cannot seem to center this text. Can someone please tell me where the error is.

NSMutableParagraphStyle *paragraphStyle = NSMutab         


        
10条回答
  •  一个人的身影
    2020-12-13 17:26

    In Swift 5

    let paragraph = NSMutableParagraphStyle()
    paragraph.alignment = .center
    textView.attributedText = NSAttributedString(string: "String",
                                                         attributes: [.paragraphStyle: paragraph])
    

    In Swift-4

    let paragraph = NSMutableParagraphStyle()
    paragraph.alignment = .center
    
    let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.paragraphStyle: paragraph]
    let attrString = NSAttributedString(string:"string", attributes: attributes)
    textView.attributedText =  attrString
    

    In Swift-3

    let paragraph = NSMutableParagraphStyle()
    paragraph.alignment = .center
    
    let attributes: [String : Any] = [NSParagraphStyleAttributeName: paragraph]
    let attrString = NSAttributedString(string:"string", attributes: attributes)
    textView.attributedText =  attrString
    

提交回复
热议问题