stringbyappendingstring different size and font types

泄露秘密 提交于 2019-12-13 03:43:19

问题


I have a NSString that is a letter and another that is their signature. I need to combine both into a single string for display but the signature needs to display in a different font and size. Yes, both fonts and sizes are working on their own.

Right now I have it as follows:

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 displays as the same size and font type.


回答1:


You're playing in the wrong ball park - looking in the wrong place - barking up the wrong tree - using the wrong class.

text and NSString and stringByAppendingString have no size or font information. The fonts of _paragraph and _signature are utterly irrelevant; they are merely ways in which those views display strings that of themselves lack any font at all. Thus, when you combine strings that have no fonts, you still wind up with a string that has no font.

If you want to work with strings that do have size and font information, you need to start working with NSAttributedString (and attributedText).

This is a single NSAttributedString:




回答2:


there is a old method,may be useful;

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;  


来源:https://stackoverflow.com/questions/26726823/stringbyappendingstring-different-size-and-font-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!