参考了网上较多的文章,感觉这篇文章看起来会实用些
http://www.jianshu.com/p/d913658c2f1f
小Tips:
文字添加中划线:
NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]
问题:按上面代码添加"¥100 "会出现中划线偏移,需要用下面代码
[str setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSForegroundColorAttributeName:_kMainColor9} range:NSMakeRange(1+price.length, yuanjia.length)];
swift
nameAstr.addAttributes([NSAttributedString.Key.strikethroughStyle : NSUnderlineStyle.single.rawValue], range: NSRange(location: 0, length: 4))
变相文字垂直居中:
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineHeightMultiple = 1.3;//行高比例调整达到垂直居中
NSParagraphStyleAttributeName:paraStyle
调整文字的基线,-3是下移3,@(2)就是上移3
NSBaselineOffsetAttributeName: @(-3)
swift 下划线:
[NSAttributedString.Key.underlineStyle: NSNumber(integerLiteral: NSUnderlineStyle.single.rawValue)]
但是还是没有解决我的问题,是不是修改已设置的AttributedString属性,就是重新再设置一遍AttributedString.
比如说
UILabel *temp = [[UILabel alloc] init];
temp.text = @"颜色";
temp.textColor = [UIColor redColor];
//如果修改颜色一般是
temp.textColor = [UIColor yellowColor];
但是如果是AttributedString
NSDictionary *attrs = @{ NSFontAttributeName: [UIFont systemFontOfSize:20] ,NSForegroundColorAttributeName: [UIColor redColor]};
textNode.attributedString = [[NSAttributedString alloc]initWithString:username attributes:attrs];
[self.view addSubiew:textNode];
//这里修改颜色,只能是
NSDictionary *attrs = @{ NSFontAttributeName: [UIFont systemFontOfSize:20] ,NSForegroundColorAttributeName: [UIColor yellowColor]};
textNode.attributedString = [[NSAttributedString alloc]initWithString:username attributes:attrs];
没有找到便捷的修改单一属性的办法,这里留个问题……