文本中心alignment
我已经尝试了一切,但似乎无法居中这个文本。 有人可以告诉我错误在哪里。
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]}];
您可以使用此设置中心alignment方式。 记得设定范围。
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-3中
let paragraph = NSMutableParagraphStyle() paragraph.alignment = .center let attributes: [String : Any] = [NSParagraphStyleAttributeName: paragraph] let attrString = NSAttributedString(string:"string", attributes: attributes) textView.attributedText = attrString
在Swift中
let titleString = "title here" let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .Center let attributedString = NSAttributedString( string: titleString, attributes: [NSParagraphStyleAttributeName: paragraphStyle] ) titleAttributedLabel.attributedText = attributedString
在斯威夫特4
let paragraph = NSMutableParagraphStyle() paragraph.alignment = .center let attributes = [NSAttributedStringKey.paragraphStyle: paragraph] let attrString = NSAttributedString(string:"string", attributes: attributes) textView.attributedText = attrString
其他方式:
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}];
在Swift 2.x中完成
let attributeString = NSMutableAttributedString(string: "text") style.alignment = .Center attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: range)
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))