112

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

NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
paragraphStyle.alignment = NSTextAlignmentCenter;
label.attributedText = [[NSAttributedString alloc] initWithString:cell.EventTitle.text attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],NSParagraphStyleAttributeName:paragraphStyle,NSBaselineOffsetAttributeName : @0,NSFontAttributeName : [UIFont fontWithName:@"BrandonGrotesque-Black" size:34]}];
Krunal
  • 77,632
  • 48
  • 245
  • 261
user3255746
  • 1,377
  • 3
  • 12
  • 13
  • 1
    check out this link....it may help you.. http://stackoverflow.com/questions/6801856/iphone-nsattributedstring-add-text-alignment – Bhavin Bhadani Mar 14 '15 at 06:11

12 Answers12

165

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
Alirza Eram
  • 134
  • 11
Shrawan
  • 7,128
  • 4
  • 29
  • 40
64

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])];
mattsson
  • 1,329
  • 1
  • 14
  • 30
user3575114
  • 993
  • 7
  • 13
39

In Swift 4

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

textView.attributedText = NSAttributedString(string: "string",
                                         attributes: [.paragraphStyle: paragraph])
Ky -
  • 30,724
  • 51
  • 192
  • 308
Er. Vihar
  • 1,495
  • 1
  • 15
  • 29
28

Another way:

Swift:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributedString = NSAttributedString(string: "This will be centered.", attributes: [ NSAttributedString.Key.paragraphStyle: paragraphStyle])

Obj-C:

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentCenter;    
NSAttributedString *attributedString =  [NSAttributedString.alloc initWithString:@"This will be centered." 
attributes: @{NSParagraphStyleAttributeName:paragraphStyle}];
johndpope
  • 5,035
  • 2
  • 41
  • 43
Hemang
  • 26,840
  • 19
  • 119
  • 186
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;
Krunal
  • 77,632
  • 48
  • 245
  • 261
16

In Swift

let titleString = "title here"

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Center

let attributedString = NSAttributedString(
    string: titleString,
    attributes: [NSParagraphStyleAttributeName: paragraphStyle]
)

titleAttributedLabel.attributedText = attributedString
aguilarpgc
  • 1,181
  • 12
  • 24
10

helper method based on the helpful answers above

public extension NSAttributedString
{
    var centered: NSAttributedString
    {
        let paragraph = NSMutableParagraphStyle()
        paragraph.alignment = .center
        let m = NSMutableAttributedString(attributedString: self)
        m.addAttribute(.paragraphStyle, value: paragraph, range: NSMakeRange(0, length))
        return m
    }
}

in case you want the Is dotted and Ts crossed el verbositas version

var centered: NSAttributedString {
        let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = NSTextAlignment.center

        let attributedString = NSMutableAttributedString(attributedString: self)
        attributedString.addAttributes([NSAttributedString.Key.paragraphStyle : paragraphStyle],
                                       range: NSRange(location: 0, length: attributedString.length))
        return attributedString
}
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
9

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))

Example

Dan Leonard
  • 3,325
  • 1
  • 20
  • 32
4

To do it in Swift 2.x

let attributeString = NSMutableAttributedString(string: "text")
style.alignment = .Center
attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: range)
Martin Romañuk
  • 1,039
  • 10
  • 14
2

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
Waseem Sarwar
  • 2,645
  • 1
  • 21
  • 18
2

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;
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
-2

This works for me label.textAlignment = .center

Mohan
  • 361
  • 1
  • 3
  • 11