#import "RegisterBtn.h"
@implementation RegisterBtn
- (instancetype)initWithFrame:(CGRect)frame
{
if(self = [super initWithFrame: frame]){
UILabel * labelOne = [[UILabel alloc ] initWithFrame: CGRectMake(0, 0, 160, 20) ];
NSMutableAttributedString * theStr = [[NSMutableAttributedString alloc ] initWithString: @"已有账号,立即登录" ];
[theStr addAttributes: @{NSFontAttributeName: [UIFont systemFontOfSize: 14], NSForegroundColorAttributeName: [UIColor whiteColor]} range: NSMakeRange(0, 5)];
[theStr addAttributes: @{NSFontAttributeName: [UIFont systemFontOfSize: 14], NSForegroundColorAttributeName: [UIColor greenColor]} range: NSMakeRange(5, 4)];
labelOne.attributedText = theStr;
[self addSubview: labelOne ];
}
return self;
}
SWIFT
let idText = model.id
let indexId = model.id?.characters.count
let strId = NSMutableAttributedString(string: "身份证: \(idText!)")
strId.addAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(23), NSForegroundColorAttributeName: UIColor.magentaColor()], range: NSMakeRange(6, indexId!))
self.idLabel.attributedText = strId
# let modelText = model.commentCount
## let indexComment = modelText?.characters.count
let stringComment = NSMutableAttributedString(string: "评论数: \(modelText!)")
stringComment.addAttributes([NSFontAttributeName: UIFont.systemFontOfSize(21), NSBackgroundColorAttributeName:UIColor.cyanColor()], range: NSMakeRange(6, indexComment!))
self.commentsLabel.attributedText = stringComment
let authorText = model.author
let indexAuthor = authorText?.characters.count
let stringAuthor = NSMutableAttributedString(string: "作者: \(authorText!)")
stringAuthor.addAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(22), NSForegroundColorAttributeName: UIColor.redColor(), NSUnderlineStyleAttributeName: 10], range: NSMakeRange(5, indexAuthor!))
其中
NSUnderlineStyleAttributeName: 10
因为
OC
typedef enum NSUnderlineStyle : NSInteger {
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09,
NSUnderlinePatternSolid = 0x0000,
NSUnderlinePatternDot = 0x0100,
NSUnderlinePatternDash = 0x0200,
NSUnderlinePatternDashDot = 0x0300,
NSUnderlinePatternDashDotDot = 0x0400,
NSUnderlineByWord = 0x8000
} NSUnderlineStyle;