NSAttributedString设置文本的位置

NSAttributedString作为一种强大的文本处理工具,常用于复杂的文本布局和格式设定。本文将深入探讨如何精准设置NSAttributedString中文本的位置,助你更好地掌握这一技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//1. 如NSAttributedString与iOS上的核心文本,你有CTParagraphStyle而不是NSParagraphStyle。没有可变的变体。 例如:
CTTextAlignment alignment = kCTCenterTextAlignment;
CTParagraphStyleSetting alignmentSetting;
alignmentSetting.spec = kCTParagraphStyleSpecifierAlignment;
alignmentSetting.valueSize = sizeof(CTTextAlignment);
alignmentSetting.value = &alignment;
CTParagraphStyleSetting settings[1] = {alignmentSetting};
CFIndex settingCount = 1;
CTParagraphStyleRef paragraphRef = CTParagraphStyleCreate(settings, settingsCount);
NSDictionary *attributes = @{(__bridge id)kCTParagraphStyleAttributeName : (__bridge id)paragraphRef};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Hello World" attributes:attributes];

//2.
NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc] init];
paragrapStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = @{
        NSParagraphStyleAttributeName: paragrapStyle
        };
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text attributes:attributes];

//3. 我正在寻找的问题,并能中心对齐在NSAttributedString的文字是这样的:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:string];
[attribString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];

//4.
  [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]];


NSAttributedString设置太常用了,功能也蛮强大的!好好学学

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值