NSAttributedString adds text alignment

How to add text alignment attribute to NSAttributedString to center the text?

Edit: Am I doing something wrong? It does not seem to change the alignment.

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]; 
+106
ios objective-c core-text nsattributedstring
Jul 23 2018-11-18T00:
source share
7 answers

Since NSAttributedString is mainly used with Core Text on iOS, you should use CTParagraphStyle instead of NSParagraphStyle . No modified option.

For example:

 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]; 
+38
Jul 23 2018-11-11T00:
source share
  NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new; paragraphStyle.alignment = NSTextAlignmentCenter; NSAttributedString *attributedString = [NSAttributedString.alloc initWithString:@"someText" attributes: @{NSParagraphStyleAttributeName:paragraphStyle}]; 
+257
Mar 13 '13 at 15:34
source share

I was looking for the same problem and was able to center the alignment of the text in NSAttributedString as follows:

 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ; [paragraphStyle setAlignment:NSTextAlignmentCenter]; NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:string]; [attribString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])]; 
+91
Oct 23 '13 at 11:41
source share

Swift 4.0+

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

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

(original answer below)

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]) 
+65
Feb 02 '16 at 13:11
source share

Swift 4 answer:

 // Define paragraph style - you got to pass it along to NSAttributedString constructor let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center // Define attributed string attributes let attributes = [NSAttributedStringKey.paragraphStyle: paragraphStyle] let attributedString = NSAttributedString(string:"Test", attributes: attributes) 
+20
Oct 19 '17 at 10:57 on
source share

In Swift 4:

  let paraStyle = NSMutableParagraphStyle.init() paraStyle.alignment = .left let str = "Test Message" let attribute = [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 12)] let attrMessage = NSMutableAttributedString(string: str, attributes: attribute) attrMessage.addAttribute(kCTParagraphStyleAttributeName as NSAttributedStringKey, value: paraStyle, range: NSMakeRange(0, str.count)) 
+2
Jul 11 '18 at 15:45
source share
 [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]]; 
-5
Jan 17 '13 at 13:03
source share



All Articles