网上搜了大半天,如出一辙的结果如下:
设置行间距和换行模式都是设置一个属性:kCTParagraphStyleAttributeName,这个属性里面又分为很多子
属性,其中就包括
kCTLineBreakByCharWrapping
kCTParagraphStyleSpecifierLineSpacingAdjustment
设置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //段落 //line break CTParagraphStyleSetting lineBreakMode; CTLineBreakMode lineBreak = kCTLineBreakByCharWrapping; //换行模式 lineBreakMode.spec = kCTParagraphStyleSpecifierLineBreakMode; lineBreakMode.value = &lineBreak; lineBreakMode.valueSize = sizeof(CTLineBreakMode); //行间距 CTParagraphStyleSetting LineSpacing; CGFloat spacing = 4.0; //指定间距 LineSpacing.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment; LineSpacing.value = &spacing; LineSpacing.valueSize = sizeof(CGFloat); CTParagraphStyleSetting settings[] = {lineBreakMode,LineSpacing}; CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, 2); //第二个参数为settings的长度 [attributedString addAttribute:(NSString *)kCTParagraphStyleAttributeName value:(id)paragraphStyle range:NSMakeRange(0, attributedString.length)]; |
—————————————–猥琐的分界线—————————————–
1 2 3 4 5 6 | 这并不是唯一的方法,还有另一种替代方案: CATextLayer *textLayer = [CATextLayer layer]; textLayer.string = getAttributedString(); textLayer.frame = CGRectMake(0, CGRectGetMaxY(view.frame), 200, 200); [self.view.layer addSublayer:textLayer]; CATextLayer可以直接支持NSAttributedString! |
—————————————–猥琐的分界线—————————————–
其实不用这么麻烦,一行代码搞定!哪怕是Button上的AttribledText,看好了别眨眼:
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;