【问题标题】:stringbyappendingstring different size and font typesstringbyappendingstring 不同的大小和字体类型
【发布时间】:2014-12-30 20:26:45
【问题描述】:

我有一个 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 显示为相同的大小和字体类型。

【问题讨论】:

    标签: ios nsstring uifont


    【解决方案1】:

    你在错误的球场打球 - 在错误的地方看 - 在错误的树上吠叫 - 使用错误的课程。

    text 和 NSString 和 stringByAppendingString没有 大小或字体信息。 _paragraph_signature 的字体完全无关;它们只是这些视图显示本身没有任何字体的字符串的方式。因此,当你组合没有字体的字符串时,你仍然会得到一个没有字体的字符串。

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

    这是一个单一的 NSAttributedString:

    【讨论】:

    • 这是我书中教你如何做的部分:apeth.com/iOSBook/ch23.html#_attributed_strings
    • 我在 UITextView 中显示最终结果,我知道它不支持 NSAttributedString。我说的对吗?
    • 如果您使用的是 iOS 5 或更低版本,那么您是正确的。否则,不,你不正确。
    • 很高兴知道!我会尝试属性字符串,谢谢。检查您的链接并希望将此答案标记为正确
    【解决方案2】:

    有个老方法,可能有用;

    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;  
    

    【讨论】:

    • 我看过这个例子但并不总是知道签名的大小并且不能使用NSMakeRange。它总是不同的大小。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 2020-09-02
    • 2015-10-29
    • 2017-12-23
    相关资源
    最近更新 更多