NSAttributedString添加文本alignment

我如何添加文本alignment属性到NSAttributedString来居中文本?

编辑:我做错了什么? 似乎没有改变路线。

CTParagraphStyleSetting setting; setting.spec = kCTParagraphStyleSpecifierAlignment; setting.valueSize = kCTCenterTextAlignment; CTParagraphStyleSetting settings[1] = { {kCTParagraphStyleSpecifierAlignment, sizeof(CGFloat), &setting}, }; CTParagraphStyleRef paragraph = CTParagraphStyleCreate(settings, sizeof(setting)); NSMutableAttributedString *mutableAttributed = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedString]; [mutableAttributed addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:(NSObject*)paragraph ,(NSString*) kCTParagraphStyleAttributeName, nil] range:_selectedRange]; 

由于NSAttributedString主要与iOS上的Core Text一起使用,因此您必须使用CTParagraphStyle而不是NSParagraphStyle 。 没有可变的变体。

例如:

 CTTextAlignment alignment = kCTCenterTextAlignment; CTParagraphStyleSetting alignmentSetting; alignmentSetting.spec = kCTParagraphStyleSpecifierAlignment; alignmentSetting.valueSize = sizeof(CTTextAlignment); alignmentSetting.value = &alignment; CTParagraphStyleSetting settings[1] = {alignmentSetting}; size_t settingsCount = 1; CTParagraphStyleRef paragraphRef = CTParagraphStyleCreate(settings, settingsCount); NSDictionary *attributes = @{(__bridge id)kCTParagraphStyleAttributeName : (__bridge id)paragraphRef}; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Hello World" attributes:attributes]; 
  NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new; paragraphStyle.alignment = NSTextAlignmentCenter; NSAttributedString *attributedString = [NSAttributedString.alloc initWithString:@"someText" attributes: @{NSParagraphStyleAttributeName:paragraphStyle}]; 

我正在寻找相同的问题,并能够以这种方式中心alignmentNSAttributedString中的文本:

 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ; [paragraphStyle setAlignment:NSTextAlignmentCenter]; NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:string]; [attribString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])]; 

Swift 3.0+

 let titleParagraphStyle = NSMutableParagraphStyle() titleParagraphStyle.alignment = .center let titleFont = UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline) let title = NSMutableAttributedString(string: "You Are Registered", attributes: [NSFontAttributeName:titleFont, NSForegroundColorAttributeName:UIColor.red, NSParagraphStyleAttributeName: titleParagraphStyle]) 

(下面的原始答案)

Swift 2.0+

 let titleParagraphStyle = NSMutableParagraphStyle() titleParagraphStyle.alignment = .Center let titleFont = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline) let title = NSMutableAttributedString(string: "You Are Registered", attributes:[NSFontAttributeName:titleFont, NSForegroundColorAttributeName:UIColor.redColor(), NSParagraphStyleAttributeName: titleParagraphStyle]) 

斯威夫特4回答:

 // Define paragraph style - you got to pass it along to NSAttributedString constructor let paragraphStyle = NSMutableParagraphStyle() titleParagraphStyle.alignment = .center // Define attributed string attributes let attributes = [.paragraphStyle: titleParagraphStyle] let attributedString = NSAttributedString(string:"Test", attributes: attributes) 
 [averagRatioArray addObject:[NSString stringWithFormat:@"When you respond Yes to %@ the average response to %@ was %0.02f",QString1,QString2,M1]]; [averagRatioArray addObject:[NSString stringWithFormat:@"When you respond No to %@ the average response to %@ was %0.02f",QString1,QString2,M0]]; UIFont *font2 = [UIFont fontWithName:@"Helvetica-Bold" size:15]; UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:12]; NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"When you respond Yes to %@ the average response to %@ was",QString1,QString2]]; [str addAttribute:NSFontAttributeName value:font range:NSMakeRange(0,[@"When you respond Yes to " length])]; [str addAttribute:NSFontAttributeName value:font2 range:NSMakeRange([@"When you respond Yes to " length],[QString1 length])]; [str addAttribute:NSFontAttributeName value:font range:NSMakeRange([QString1 length],[@" the average response to " length])]; [str addAttribute:NSFontAttributeName value:font2 range:NSMakeRange([@" the average response to " length],[QString2 length])]; [str addAttribute:NSFontAttributeName value:font range:NSMakeRange([QString2 length] ,[@" was" length])]; // [str addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(49+[QString1 length]+[QString2 length] ,8)]; [averagRatioArray addObject:[NSString stringWithFormat:@"%@",str]];