Alinhamento de centro de texto atribuído


86

Eu tentei de tudo, mas não consigo centralizar este texto. Alguém pode me dizer onde está o erro.

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]}];

1
confira este link ... pode ajudá-lo ... stackoverflow.com/questions/6801856/…
EI Captain v2.0

Respostas:


134

Em Swift 5

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

Em 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

Em Swift-3

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

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

1
NSParagraphStyleAttributeName -> NSAttributedStringKey.paragraphStyle
Abhijith

62

Você pode definir o alinhamento central usando isso. Lembre-se de definir o alcance.

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];

38

Em Swift 4

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

textView.attributedText = NSAttributedString(string: "string",
                                         attributes: [.paragraphStyle: paragraph])

20

Outra maneira:

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}];

16

In Swift

let titleString = "title here"

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

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

titleAttributedLabel.attributedText = attributedString

15

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;

1
Você se esqueceu de adicionar o atributo à string no exemplo ObjC :)
JordanC

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

Exemplo


4

Para fazer isso em Swift 2.x

let attributeString = NSMutableAttributedString(string: "text")
style.alignment = .Center
attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: range)

2

Às vezes, quando o texto está em árabe ou em outros idiomas de alinhamento à direita, ao fazer o alinhamento Justificado, o texto da última linha termina no lado esquerdo. para isso, podemos adicionar baseWritingDirection abaixo é um código de amostra

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .justified
paragraphStyle.baseWritingDirection = .rightToLeft
attribute.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:range)
txtView.attributedText = attribute

1

Defina o modo de quebra de linha, se você definir o texto atribuído em 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;
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.