iOS:同一个单词的Bold和Italic

时间:2012-04-09 19:21:18

标签: iphone ios xcode bold italic

背景:我一直在尝试使用Bold和Italic字体以及普通字体显示句子。

问题:如何显示“Hello,我的名字 字节 ”之类的内容。请注意,Byte既是粗体又是斜体,而其他单词仍然正常。

我试过:我认为coreText应该能够做到这一点,我只是无法找到正确的方法来做到这一点。我也使用了TTTAttributeLabel,并且不能使它既粗体又斜体。我有Three20加载,只是不知道使用哪个或什么。 Webview不适用于我的背景。


作为对Carles Estevadeordal的回复

UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 360, 300, 40)];
[webView setBackgroundColor: [UIColor clearColor]];
[webView loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: transparent;\">Hello, my name is <b><i>Byte</b></i></body></html>"] baseURL:nil];
[self.view addSubview:webView];

这正是我用过的代码。它显示白色背景。

5 个答案:

答案 0 :(得分:7)

经过一夜安眠,我找到了一种方法,使用TTTAtributedlabel。 方法如下:

TTTAttributedLabel *attLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(x, y, xx, yy)];
NSString *text = @"Hello, my name is Byte";

[attLabel setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^(NSMutableAttributedString *mutableAttributedString) {

    //font helvetica with bold and italic 
    UIFont *boldSystemFont = [UIFont fontWithName:@"Helvetica-BoldOblique" size:10];

    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);

    NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"Byte" options:NSCaseInsensitiveSearch];
    [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];

    CFRelease(font);

    return mutableAttributedString;
}];

我仍然无法将2个属性(即:粗体和斜体)分别添加到同一个单词/字母中。但这就是诀窍。

答案 1 :(得分:2)

如果您仍然对此问题的CoreText解决方案感兴趣:

newFont = CTFontCreateCopyWithSymbolicTraits(fontRef,
                                             self.textSize,
                                             NULL,
                                             kCTFontBoldTrait | kCTFontItalicTrait,
                                             kCTFontBoldTrait | kCTFontItalicTrait);

[attrString addAttribute:(NSString*)kCTFontAttributeName
                   value:(id)newFont
                   range:[copyString rangeOfString:customText.text]];

答案 2 :(得分:2)

你可以在发件人是UIButton的实例

的地方使用它
   sender.titleLabel.font=[UIFont fontWithName:@"Helvetica-BoldOblique" size:18.0f];

答案 3 :(得分:1)

添加两个属性(这是非ARC代码):

#define kLabelFontSize 16.0

NSString labelString = @"Let's slide loudly";

[self.tttLabel setText:labelString afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {

    UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:kLabelFontSize];
    CTFontRef boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
    if (boldFont) {
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:NSMakeRange(6, 11)];
        CFRelease(boldFont);
    }

    UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:kLabelFontSize];
    CTFontRef italicFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, italicSystemFont.pointSize, NULL);
    if (italicFont) {
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)italicFont range:NSMakeRange(12, 18)];
        CFRelease(italicFont);
    }

    }];

    return mutableAttributedString;
}];

答案 4 :(得分:0)

如果句子具有固定格式,最简单的解决方案是使用2个不同的UILabel,一个使用普通字体,另一个使用Bold和Italic字体集。

如果句子是动态的,那么你应该使用一个UIWebView对象,因为它是一个lavel并加载一个简单的网页,其中包含你的文本和&lt; b> &LT;我&gt; &LT; / i&gt; &LT; / b&gt;当你想要它的粗体和斜体时,在现场设置的字段如下:

[myWebViewLabel loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: transparent;\">Hello, my name is <b><i>Byte</b></i></body></html>"] baseURL:nil];

我希望它有所帮助。