2015-03-14 3 views
38

Я пробовал все, но не мог бы центрировать этот текст. Может кто-нибудь, пожалуйста, скажите мне, где ошибка.Выравнивание выделенного текстового центра

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

проверить эту ссылку .... это может помочь вам .. http://stackoverflow.com/questions/6801856/iphone-nsattributedstring-add-text-alignment –

ответ

48

Вы можете установить центральное выравнивание, используя это. Не забудьте установить диапазон.

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

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

Чтобы сделать это в Swift 2.x

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

Другой способ:

Swift:

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

Obj-C:

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new]; 
paragraphStyle.alignment = NSTextAlignmentCenter;  
NSAttributedString *attributedString = [NSAttributedString.alloc initWithString:@"This will be centered." 
attributes: @{NSParagraphStyleAttributeName:paragraphStyle}]; 
12

В Swift

let titleString = "title here" 

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

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

titleAttributedLabel.attributedText = attributedString 
52

В Swift-3

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

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

В Swift 4

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

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

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 paragraphStyle = NSMutableParagraphStyle() 
paragraphStyle.alignment = .center // center the text 
paragraphStyle.lineSpacing = 14 //Change spacing between lines 
paragraphStyle.paragraphSpacing = 38 //Change space between paragraphs 
attributedString.addAttributes([NSAttributedStringKey.paragraphStyle: paragraphStyle], range: NSRange(location: 0, length: attributedString.length)) 

Example

Смежные вопросы