ios - Alineación de centro de texto atribuido
xcode label (9)
He intentado todo pero parece que no puedo centrar este texto. ¿Puede alguien decirme dónde está el error?
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]}];
A veces, cuando el texto está en árabe u otros idiomas de alineación a la derecha, cuando se realiza la alineación Justificado, el texto de la última línea termina en el lado izquierdo. para esto podemos agregar baseWritingDirection a continuación es un código de muestra
let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .justified paragraphStyle.baseWritingDirection = .rightToLeft attribute.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:range) txtView.attributedText = attribute
De otra manera:
Rápido :
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSAttributedString *attributedString = [NSAttributedString.alloc initWithString:@"This will be centered."
attributes: @{NSParagraphStyleAttributeName:paragraphStyle}];
Obj-C :
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))
En veloz
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
Para hacerlo en Swift 2.x
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .justified
paragraphStyle.baseWritingDirection = .rightToLeft
attribute.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:range)
txtView.attributedText = attribute
Puede establecer la alineación central usando esto. Recuerde establecer el rango.
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
textView.attributedText = NSAttributedString(string: "string",
attributes: [.paragraphStyle: paragraph])
En Swift 4
let titleString = "title here"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Center
let attributedString = NSAttributedString(
string: titleString,
attributes: [NSParagraphStyleAttributeName: paragraphStyle]
)
titleAttributedLabel.attributedText = attributedString
En Swift 5
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
En Swift-4
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let attributes: [String : Any] = [NSParagraphStyleAttributeName: paragraph]
let attrString = NSAttributedString(string:"string", attributes: attributes)
textView.attributedText = attrString
En Swift-3
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
Swift 4+
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;
C objetivo
let paragraphStyle = NSMutableParagraphStyle.init()
paragraphStyle.alignment = .center
let attributedString = NSAttributedString.init(string: "This will be centered.", attributes: [NSParagraphStyleAttributeName: paragraphStyle])