stringbyappendingstring不同的大小和字体类型

时间:2014-11-04 02:34:17

标签: ios nsstring uifont

我有一个NSString是一封信,另一个是他们的签名。我需要将两者组合成一个字符串进行显示,但签名需要以不同的字体和大小显示。是的,字体和大小都是独立的。

现在我有以下内容:

NSString *letter;
NSString *signOff;

_paragraph.font = [UIFont fontWithName:@"BeinetCondensed" size:14];
_signature.font = [UIFont fontWithName:@"AnnoyingKettle" size:18];

letter = [_paragraph text];
signOff = [_signature text];

NSString *completedLetter = [letter stringByAppendingString:signOff];

completedLetter显示为相同的大小和字体类型。

2 个答案:

答案 0 :(得分:1)

你正在错误的球场打球 - 看错了地方 - 咆哮错误的树 - 使用错误的等级。

text和NSString以及stringByAppendingString没有大小或字体信息。 _paragraph_signature的字体完全不相关;它们只是这些视图显示本身缺少任何字体的字符串的方式。因此,当您组合没有字体的字符串时,您仍然可以使用没有字体的字符串。

如果您想使用具有大小和字体信息的字符串,您需要开始使用NSAttributedString(和attributedText)。

这是一个NSAttributedString:

enter image description here

答案 1 :(得分:0)

有一种旧方法,可能有用;

self.title = @"For iOS 6 & later";  
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];  
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];  
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];  
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];  
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];  
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];  
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];  
attrLabel.attributedText = str;