Attributed Text Center Alignment

前端 未结 10 2444
故里飘歌
故里飘歌 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:12

    Sometimes when text is in Arabic or other right align languages then when doing alignment Justified, last line text ends at left side. for this we can add baseWritingDirection below is sample code

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .justified
    paragraphStyle.baseWritingDirection = .rightToLeft
    attribute.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:range)
    txtView.attributedText = attribute
    
    0 讨论(0)
  • 2020-12-13 17:14

    To do it in Swift 2.x

    let attributeString = NSMutableAttributedString(string: "text")
    style.alignment = .Center
    attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: range)
    
    0 讨论(0)
  • 2020-12-13 17:18

    Swift 4+

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = NSTextAlignment.center
    
    // Swift 4.2++
    let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedString.Key.paragraphStyle:paragraphStyle])
    
    // Swift 4.1--
    let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedStringKey.paragraphStyle:paragraphStyle])
    
    let yourLabel = UILabel()
    yourLabel.attributedText = attributedString
    

    Objective-C

    NSString *string = @"Your String";
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string attributes: @{NSParagraphStyleAttributeName:paragraphStyle}];
    UILabel *label = [[UILabel alloc] init];
    label.attributedText = attributedString;
    
    0 讨论(0)
  • Swift4

    let attributedString = NSMutableAttributedString(string: "Example text that is centered using a paragraph style. With the ability to change the width between lines.", attributes: [NSAttributedStringKey.font: GothamFont.medium(with: 14)])
    
    let myParagraphStyle = NSMutableParagraphStyle()
    myParagraphStyle.alignment = .center // center the text
    myParagraphStyle.lineSpacing = 14 //Change spacing between lines
    myParagraphStyle.paragraphSpacing = 38 //Change space between paragraphs
    attributedString.addAttributes([.paragraphStyle: myParagraphStyle], range: NSRange(location: 0, length: attributedString.length))
    

    0 讨论(0)
  • 2020-12-13 17:19

    Set line breakmode, if you set attributed text on UIButton.

    Swift 5

    let paragraph = NSMutableParagraphStyle()
    paragraph.alignment = .center
    paragraph.lineBreakMode = .byClipping
    

    Objective-C

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentCenter;
    style.lineBreakMode = NSLineBreakByClipping;
    
    0 讨论(0)
  • 2020-12-13 17:24

    You can set the center alignment using this. Remember to set range.

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
    
    0 讨论(0)
提交回复
热议问题