效果图
富文本下划线.png
self.canEditLb = [UILabel new];
self.canEditLb.userInteractionEnabled = YES;
NSString *canEditText = @"可重新编辑";
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:canEditText];
[attrStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:14.0f]
range:NSMakeRange(0, 5)];
[attrStr addAttribute:NSForegroundColorAttributeName
value:UIColorFromRGB(0x1ecfdc)
range:NSMakeRange(1, 4)];
[attrStr addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
range:NSMakeRange(1, 4)];
self.canEditLb.attributedText = attrStr;
NSStrikethroughStyleAttributeName(删除线)
NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值:
NSUnderlineStyleNone 不设置删除线
NSUnderlineStyleSingle 设置删除线为细单实线
NSUnderlineStyleThick 设置删除线为粗单实线
NSUnderlineStyleDouble 设置删除线为细双实线
默认值是NSUnderlineStyleNone。
- NSRange range = [@"123456" rangeOfString:@"456"];
例子2
+(NSMutableAttributedString *)attriubteWithDestnationStr:(NSString *)destText sourceStr:(NSString *)srcText foregroundColor:(UIColor *)color{
NSMutableAttributedString* reply1AttributeStr = [[NSMutableAttributedString alloc] initWithString:destText];
NSRange range = [destText rangeOfString:srcText];
[reply1AttributeStr addAttribute:NSForegroundColorAttributeName value:color range:range];
return reply1AttributeStr;
例子3
/**
* 富文本
@param title 文字
*/
-(NSMutableAttributedString *)attributeTitle:(NSString *)title{
NSString *holderText = title;
NSRange range = [title rangeOfString:@"选择"];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:holderText];
[attrStr addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:14.0f]
range:range];
[attrStr addAttribute:NSForegroundColorAttributeName
value:UIColorFromRGB(0x4fc3f7)
range:range];
[attrStr addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
range:range];
return attrStr;
}
网友评论