mac开发给字符串加入下划线的方法。

来源:互联网 发布:手机视频制作软件 编辑:程序博客网 时间:2024/06/17 12:55

- (void) updateButtonTitle

{

@try {

NSString* titleString = [self title];

NSMutableAttributedString *hyperLinkString = [[NSMutableAttributedString allocinitWithString: titleString];

NSRange selectedRange = {0, [hyperLinkString length]};

 

NSDictionary *dict = [[self attributedTitle] attributesAtIndex:0 effectiveRange:&selectedRange];

NSFont *font = [NSFont fontWithName:@"Arial" size:12];

 

// if(dict && [dict objectForKey:@"NSFont"])

// {

// font = [dict objectForKey:@"NSFont"];

// }

 

[hyperLinkString beginEditing];

 

[hyperLinkString addAttribute:NSForegroundColorAttributeName

value:[NSColor colorWithCalibratedRed:0.8086 green:0.8086 blue:0.8086 alpha:1.0// 更改颜色

range:selectedRange];

 

[hyperLinkString addAttribute:NSUnderlineStyleAttributeName

value:[NSNumber numberWithInt:NSSingleUnderlineStyle] // 添加下化线

range:selectedRange];

 

[hyperLinkString addAttribute:NSFontAttributeName 

value:font

range:selectedRange];

 

[hyperLinkString endEditing];

 

[self setAttributedTitle: hyperLinkString];

[self sizeToFit];

 

[hyperLinkString release];

}

@catch (NSException * e) {

 

}

@finally {

 

}

}

0 0